我在Spring Boot中使用大气,但是无法使用存储库。
我尝试在Google上进行搜索,并为此花费了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);
}
答案 0 :(得分:0)
这是我的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);
}
}
}