Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在服务器启动期间,Spring boot@Autowired在servlet中不工作_Spring_Spring Boot - Fatal编程技术网

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

在服务器启动期间,Spring boot@Autowired在servlet中不工作,spring,spring-boot,Spring,Spring Boot,我有一个servlet,其启动时加载属性为“1”,在这个servlet中,我必须在应用程序服务器启动期间缓存数据库条目。 在这个servlet中,我调用了CacheService,它检索数据库对象,在应用程序启动期间使用@Autowired annotation对其进行注释,CacheService对象为null。我使用@Service annotation对CacheService进行了注释。@Autowired注释不起作用 @Service public class CacheService

我有一个servlet,其启动时加载属性为“1”,在这个servlet中,我必须在应用程序服务器启动期间缓存数据库条目。 在这个servlet中,我调用了CacheService,它检索数据库对象,在应用程序启动期间使用@Autowired annotation对其进行注释,CacheService对象为null。我使用@Service annotation对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;
        }
}
@服务
公共类缓存服务{
@自动连线
私人ijobservice jobservice impl;
公共列表getALLJobs(){
List alljobs=jobsservicepimpl.findAllJobs();
归还所有工作;
}
}
公共类StartupServlet扩展了HttpServlet{
@自动连线
private CacheService CacheService;--对象为空,未自动连接
}
下面是主要课程
@启用缓存
@EnableJpaRepositories(basePackages={“com.example.demo.repository”})
@EntityScan(basePackages={“com.example.demo.entity”})
@SpringBoot应用程序
@ComponentScan(basePackages={“com.example.demo”})
@EnableAutoConfiguration(排除={
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration.class})
公共类DemoApplication扩展SpringBootServletInitializer实现WebApplicationInitializer{
公共静态void main(字符串[]args){
run(DemoApplication.class,args);
}
@凌驾
受保护的SpringApplicationBuilder配置(SpringApplicationBuilder){
返回builder.sources(DemoApplication.class);
}
@豆子
ServletRegistrationBean myServletRegistration(){
ServletRegistrationBean srb=新的ServletRegistrationBean();
srb.setServlet(新的StartupServlet());
srb.setUrlMappings(Arrays.asList(“/path2/*”));
srb.setLoadOnStartup(1);
返回srb;
}
}

有人能在这方面帮我吗?

你应该为此做一些额外的工作。您必须与类似spring组件的
beanFactory
,并要求它将该特定实例作为合格的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();
        }
    }
}

您正在使用
new
创建servlet,因此需要提供其依赖项。由于您混合使用了注释和java代码配置,因此可以这样完成:

public class StartupServlet extends HttpServlet {

  private CacheService cacheService;

  public StartupServlet(CacheService cacheService) {
    this.cacheService = cacheService;
  }

  // ... rest of servlet
}
主要类别:

   @Bean
   ServletRegistrationBean myServletRegistration (CacheService cacheService) { // <<<--- cacheService will be provided here by Spring because it's annotated
        ServletRegistrationBean srb = new ServletRegistrationBean();
        srb.setServlet(new StartupServlet(cacheService));
        srb.setUrlMappings(Arrays.asList("/path2/*"));
        srb.setLoadOnStartup(1);
        return srb;
    }
@Bean

ServletRegistrationBean myServletRegistration(缓存服务缓存服务){//为什么要使用servlet来执行此操作?只需在常规singleton bean的PostConstruct中执行即可。@jbnitet在服务器启动期间是否执行用PostConstruct注释的方法?您的servlet不是spring管理的bean。没有自动连接possible@Jens有什么办法可以让它成为spring管理的或者我必须想出的任何其他方法吗,kin建议阅读spring MVC,确保CatcheService类位于com.example.demo包中,因为它查找@ComponentScan
   @Bean
   ServletRegistrationBean myServletRegistration (CacheService cacheService) { // <<<--- cacheService will be provided here by Spring because it's annotated
        ServletRegistrationBean srb = new ServletRegistrationBean();
        srb.setServlet(new StartupServlet(cacheService));
        srb.setUrlMappings(Arrays.asList("/path2/*"));
        srb.setLoadOnStartup(1);
        return srb;
    }