Configuration beans are not initialized

I don’t quite understand how this can be, but methods marked as @Bean are not initialized when I run them through the WebApplicationInitializer. In the debugger, you can only get to the @Autowired setters inside the @Configuration classes, but I don’t see the initialization of the beans. When I start Spring context with Junit, it works fine.

Exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

WebServiceDispatcherServletInitializer

public class WebServiceDispatcherServletInitializer implements WebApplicationInitializer {

    private static final String SERVICE_DISPATCHER_SERVLET = "service_dispatcher_servlet";

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(WebServiceConfig.class, RootConfig.class, PersistenceConfig.class);
        servletContext.addListener(new ContextLoaderListener(context));

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new CXFServlet());
        dispatcher.addMapping("/services/*");
    }
}

UPD: PersistenceConfig

@Configuration
@ComponentScan("com.softcomputer")
@PropertySource({"classpath:persistence-postgres.properties"})
@EnableTransactionManagement
public class PersistenceConfig {

    private Environment environment;

    @Autowired
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory);
        return transactionManager;
    }


    @Bean
    @Profile("postgres")
    public DataSource postgresDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(requireNonNull(environment.getProperty("jdbc.postgres.driverClassName")));
        dataSource.setUrl(requireNonNull(environment.getProperty("jdbc.postgres.connection_url")));
        dataSource.setUsername(requireNonNull(environment.getProperty("jdbc.postgres.username")));
        dataSource.setPassword(requireNonNull(environment.getProperty("jdbc.postgres.password")));

        return dataSource;
    }

    @Bean
    @Profile("postgres")
    public LocalSessionFactoryBean postgresSessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(postgresDataSource());
        sessionFactory.setPackagesToScan(
                new String[]{"com.softcomputer", "com.softcomputer.testdomain"});
        sessionFactory.setHibernateProperties(postgresAdditionalProperties());

        return sessionFactory;
    }

    private Properties postgresAdditionalProperties() {
        final Properties hibernateProperties = new Properties();

        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", requireNonNull(environment.getProperty("hibernate.postgres.hbm2ddl.auto")));
        hibernateProperties.setProperty("hibernate.dialect", requireNonNull(environment.getProperty("hibernate.postgres.dialect")));
        hibernateProperties.setProperty("hibernate.show_sql", requireNonNull(environment.getProperty("hibernate.postgres.show_sql")));
        hibernateProperties.setProperty("hibernate.default_schema", requireNonNull(environment.getProperty("hibernate.postgres.default_schema")));
        hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", requireNonNull(environment.getProperty("hibernate.postgres.globally_quoted_identifiers")));

        return hibernateProperties;
    }
}

UPD1: Beans in WebServiceConfig.class are not initialized either. But RootConfig.class is initialized fine (all config classes has @Configuration annotation)

UPD2: Maybes WebApplicationInitializer.onStartup expect another configuration, as szatkus said. Then what kind of startup should be used to run new CXFServlet() with such Configuration classes?

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

LocalSessionFactoryBean doesn’t implement SessionFactory interface. Maybe try:

@Bean
@Autowired
public SessionFactory sessionFactory(LocalSessionFactoryBean sessionFactoryBean) {
    return bean.getObject();
}