虽然我的应用程序是一个Web应用程序,但它并没有使用Spring的很多Web控制器。它只是REST(平针织)和插座。我的应用程序中只有大约一半使用依赖注入。我的应用程序在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");
}
答案 0 :(得分:3)
我认为由于静态背景,没有办法做到这一点。
如果您可以以非静态方式使用init()
方法,那么您可以使用SpringBeanAutowiringSupport
帮助程序类来执行此操作:
@Autowired
private ProductionService productionService;
// ... another dependencies
public void init() throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
// use autowired services
}
还有另一种选择:您可以使用SpringBeanAutowiringSupport
作为Production
类的基类。在这种情况下,您无需手动调用SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
。只需添加依赖项即可。