虽然我的应用程序是一个web应用程序,但它并不使用Spring的web控制器。只是休息(运动衫)和插座。只有大约一半的应用程序使用依赖注入。我的应用程序是在我的main()中初始化的。
ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new SpringServlet());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.production.resource");
jerseyServletRegistration.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters", "com.production.resource.ResponseCorsFilter");
jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.feature.DisableWADL", Boolean.TRUE.toString());
jerseyServletRegistration.setInitParameter("org.codehaus.jackson.map.DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY", Boolean.TRUE.toString());
jerseyServletRegistration.setLoadOnStartup(1);
jerseyServletRegistration.addMapping("/api/*");
//add atmosphere support
ServletRegistration socketRegistration = ctx.addServlet("AtmosphereServlet", new SocketInitializer());
socketRegistration.setLoadOnStartup(1);
//socketRegistration.addMapping("/socket/*");
//deploy
logger.info("Deploying server...");
ctx.deploy(server);
server.start();
//start the production process
Production.init();
System.in.read();
server.stop();在Production类中,我通过ApplicationContextProvider加载服务。是否有更好的方法可以通过依赖注入加载所有资源?
public static void init() {
//add hook to trigger Production Shutdown sequence
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
Production.shutdown();
}
}));
logger.info("Initializing production workflows...");
productionService = (ProductionService) ApplicationContextProvider.getApplicationContext().getBean("productionService");
//load active workflows into memory
WorkflowService workflowService = (WorkflowService) ApplicationContextProvider.getApplicationContext().getBean("workflowService");
for (WorkflowEntity workflowEntity : workflowService.findActive()) {
logger.info(" - "+workflowEntity.getName()+"");
workflowList.add(Workflow.factory(workflowEntity));
}
bootup();
logger.info("Production initialized");
}发布于 2013-07-16 12:19:06
我认为没有办法做到这一点,因为静态的背景。
如果可以以非静态的方式使用init()方法,那么可以使用SpringBeanAutowiringSupport助手类:
@Autowired
private ProductionService productionService;
// ... another dependencies
public void init() throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
// use autowired services
}还有一个选项:您可以使用SpringBeanAutowiringSupport作为Production类的基类。在这种情况下,您不需要手动调用SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);。只需添加依赖项并运行即可。
https://stackoverflow.com/questions/17676025
复制相似问题