我正在使用 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/