0

I have this class that creates a socket on the java side. The problem is that it's throwing a java.lang.IllegalArgumentException: No bean found for class com.production.workflow.process.approval.ApprovalSocketHandler

package com.production;

@WebListener
public class SocketInitializer implements ServletContextListener {

    public static App app;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        AutowireCapableBeanFactory beanFactory = ApplicationContextProvider.getApplicationContext().getAutowireCapableBeanFactory();
        app =  new App(new Options().url("/socket/workstation/approval").packageOf(this), new AtmosphereModule(servletContextEvent.getServletContext()), new SpringModule(beanFactory));
        app.bean(ApprovalSocketHandler.class).init();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

The package that this class is in, is indeed being scanned via my config. I suspect that at the point this Listener is being initialized that config hasn't yet scanned.

@Configuration
@ComponentScan(basePackages = {
        "com.production"
})
@PropertySource(value= {
        "classpath:/application.properties",
        "classpath:/environment-${MY_ENVIRONMENT}.properties"
})
@EnableJpaRepositories("com.production.repository")
@EnableTransactionManagement
public class Config {
    @Value("${db.url}")
    String PROPERTY_DATABASE_URL;
    @Value("${db.user}")
    String PROPERTY_DATABASE_USER;
    @Value("${db.password}")
    String PROPERTY_DATABASE_PASSWORD;

    @Value("${persistenceUnit.default}")
    String PROPERTY_DEFAULT_PERSISTENCE_UNIT;

    @Value("${hibernate.dialect}")
    String PROPERTY_HIBERNATE_DIALECT;
    @Value("${hibernate.format_sql}")
    String PROPERTY_HIBERNATE_FORMAT_SQL;
    @Value("${hibernate.show_sql}")
    String PROPERTY_HIBERNATE_SHOW_SQL;
    @Value("${entitymanager.packages.to.scan}")
    String PROPERTY_ENTITYMANAGER_PACKAGES_TO_SCAN;

    @Bean
    public App app() {
        return SocketInitializer.app;
    }

What do I need to do in order to ensure this class is a bean during ServletContextListener execution?

4

1 回答 1

1

界面ServletContextAware就是你要找的。它提供了方法

setServletContext(ServletContext servletContext) 

Spring 将使用它来注入应用程序上下文。在您的情况下,这将由ServletContext您的 servlet 容器创建。

你不应该ServletContextListener在你的问题中使用它,因为 aServletContextListener是由 servlet 容器而不是 Spring 初始化的。因此,春天不能发挥它的魔力。

于 2013-04-29T16:10:56.913 回答