gpt4 book ai didi

Spring boot @Autowired 在服务器启动期间在 servlet 中不起作用

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

我有一个 servlet,其启动时负载属性为“1”,在此 servlet 中,我必须在应用程序服务器启动期间缓存数据库条目。在此 servlet 中,我调用 CacheService 来检索 db 对象,其使用 @Autowired 注释进行注释,在应用程序启动期间,CacheService 对象为 null。我已使用 @Service 注释对 CacheService 进行注释。 @Autowired 注释不起作用。

@Service
public class CacheService {
@Autowired
private IJobsService jobsServiceImpl;
public List<Jobs> getALLJobs(){
List<Jobs> alljobs = jobsServiceImpl.findAllJobs();
return alljobs;
}

}


public class StartupServlet extends HttpServlet {

@Autowired
private CacheService cacheService; -- object is null not autowired
}

Below is the main class


@EnableCaching
@EnableJpaRepositories(basePackages = {"com.example.demo.repository"})
@EntityScan(basePackages = {"com.example.demo.entity"})
@SpringBootApplication
@ComponentScan(basePackages={"com.example.demo"})
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration.class})

public class DemoApplication extends SpringBootServletInitializer implements WebApplicationInitializer{

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}

@Bean
ServletRegistrationBean myServletRegistration () {
ServletRegistrationBean srb = new ServletRegistrationBean();
srb.setServlet(new StartupServlet());
srb.setUrlMappings(Arrays.asList("/path2/*"));
srb.setLoadOnStartup(1);
return srb;
}
}

有人可以帮我解决这个问题吗?

最佳答案

您应该为此做一些额外的工作。您必须与类似 beanFactory 的 spring 组件通信,并要求它使该特定实例成为合格的 bean。 AutowireCapableBeanFactory 应该可以解决问题。

这是一个基于您提供的代码的简单示例

@SpringBootApplication
public class So44734879Application {

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

@Autowired
AutowireCapableBeanFactory beanFactory;

@Bean
ServletRegistrationBean myServletRegistration() {
ServletRegistrationBean srb = new ServletRegistrationBean();
final StartupServlet servlet = new StartupServlet();
beanFactory.autowireBean(servlet); // <--- The most important part
srb.setServlet(servlet);
srb.setUrlMappings(Arrays.asList("/path2/*"));
srb.setLoadOnStartup(1);
return srb;
}

@Bean
MyService myService() {
return new MyService();
}

public static class MyService {
String time() {
return "Time: " + System.currentTimeMillis();
}
}

public static class StartupServlet extends HttpServlet {
@Autowired
MyService myService;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final PrintWriter writer = resp.getWriter();
writer.write(myService.time());
writer.close();
}
}
}

=>

$ curl -XGET localhost:8080/path2
Time: 1498299772141%

关于Spring boot @Autowired 在服务器启动期间在 servlet 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44734879/

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