配置Bean未初始化

时间:2019-08-01 13:21:28

标签: java spring cxf

我不太了解如何实现,但是当我通过WebApplicationInitializer运行标记为@Bean的方法时,它们不会初始化。在调试器中,您只能访问@Autowired类中的@Configuration设置程序,但看不到Bean的初始化。当我使用Junit启动Spring上下文时,它工作正常。

例外:

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 WebServiceConfig.class中的Bean也未初始化。但是RootConfig.class初始化很好(所有配置类都有@Configuration批注)

UPD2 :也许WebApplicationInitializer.onStartup需要另一个配置,如 szatkus 所述。那么应该使用哪种启动方式来使用此类Configuration类运行new CXFServlet()

1 个答案:

答案 0 :(得分:0)

LocalSessionFactoryBean未实现SessionFactory接口。也许尝试:

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