gpt4 book ai didi

java - UserRepository 在 @Autowired 中返回 null

转载 作者:行者123 更新时间:2023-12-02 03:03:51 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com