java - UserRepository 在 @Autowired 中返回 null

标签 java spring-data-jpa atmosphere

我正在使用 Spring Boot 的 Atmosphere ,但无法使用存储库。

我尝试在谷歌上搜索并花了 4-5 个小时。如果你知道请帮助我。提前致谢

这是我的代码

import ...

@AtmosphereHandlerService(path = "/stream",
        interceptors= {AtmosphereResourceLifecycleInterceptor.class,
                BroadcastOnPostAtmosphereInterceptor.class})
@Service
public class ServerService extends OnMessage<String> {
    private final Logger logger = LoggerFactory.getLogger(ServerService.class);
    private final ConcurrentHashMap<String, UserProtocol> users = new ConcurrentHashMap<String, UserProtocol>();
    private final ConcurrentHashMap<String, GuestProtocol> guests = new ConcurrentHashMap<String, GuestProtocol>();
    private final ObjectMapper mapper = new ObjectMapper();
    @Autowired
    private UserRepository userRepository; //always null
    @Override
    public void onMessage(AtmosphereResponse response, String message) throws IOException {
        logger.info(message);
        String uuid = response.uuid();
        logger.info(uuid);
       // UserRepository userRepository = new
        AtmosphereResource r = response.resource();
        List<User> users = userRepository.findUserById((long) 1);

//        logger.info(test.getName());
        logger.info("aa");

    }
}

package com.a.server.repository;
import java.util.List;

import com.a.server.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findUserById(Long id);
}

最佳答案

这是我的 Spring 应用程序类。


@SpringBootApplication
@EnableAutoConfiguration
public class ServerApplication {
    private final Logger logger = LoggerFactory.getLogger(ServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }


    @Bean
    public EmbeddedAtmosphereInitializer atmosphereInitializer() {
        return new EmbeddedAtmosphereInitializer();
    }

    @Bean
    public ServletRegistrationBean atmosphereServlet() {
        // Dispatcher servlet is mapped to '/home' to allow the AtmosphereServlet
        // to be mapped to '/chat'
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new AtmosphereServlet(), "/stream");
        registration.addInitParameter("org.atmosphere.cpr.packages", "sample");
        registration.addInitParameter("org.atmosphere.interceptor.HeartbeatInterceptor"
                + ".clientHeartbeatFrequencyInSeconds", "10");
        registration.setLoadOnStartup(0);
        // Need to occur before the EmbeddedAtmosphereInitializer
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    @Configuration
    static class MvcConfiguration extends WebMvcConfigurerAdapter {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("");
        }

    }

    private static class EmbeddedAtmosphereInitializer extends ContainerInitializer
            implements ServletContextInitializer {

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            onStartup(Collections.<Class<?>> emptySet(), servletContext);
        }

    }


}

关于java - UserRepository 在 @Autowired 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57034494/

相关文章:

java - JSF 1.2 : java. util.ConcurrentModificationException

java - Apache Spark 触发 RDD 转换的最便宜方法

java - 连接到URL失败,但是当我直接在浏览器中尝试时,它成功了

hibernate - WebSocket(Atmosphere)与每个请求问题(Hibernate 和 Shiro)- Vaadin

GWT Atmosphere + RequestFactory

javascript - Websocket 帧大小限制

java - 如何刷新 JFrame 中包含的按钮的颜色?

java - 正确使用单个 @ManyToOne 关联进行大小控制和分页

java - 为什么将 @Repository 放在 Spring Data JPA 接口(interface)之上?

java - 如何仅使用关联对象的外键来保存 Spring 实体