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");
}
4

1 回答 1

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);。只需添加依赖项即可。

于 2013-07-16T12:19:06.737 回答