1
雖然我的應用程序是一個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");
}
如果我不想一直提供服務,但只爲特定方法提供服務,是否有更多按需方式自動裝配「ApplicationContextProvider」之外的類? – Webnet
我不知道更多的點播方式來做到這一點。而要使用'SpringBeanAutowiringSupport',你需要移除'static'關鍵字。其實它不會爲你開箱。 –