IllegalArgumentException:导入类中不存在@EnableTransactionManagement

时间:2014-09-09 16:11:54

标签: java spring inheritance spring-java-config

我试图以通用的方式在我的应用程序中配置所有spring应用程序上下文 我有GeneralDSConfiguration类,这是一般配置

@Configuration
@EnableTransactionManagement
public class GeneralDSConfiguration implements InitializingBean {

    private String databaseURL;
    private String databaseUserName;
    private String databasePassword;

    public GeneralDSConfiguration(String databaseURL, String databaseUserName, String databasePassword) {
        this.databaseURL = databaseURL;
        this.databaseUserName = databaseUserName;
        this.databasePassword = databasePassword;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(databaseURL);
    }

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setAccessToUnderlyingConnectionAllowed(true);
        ds.setUrl(databaseURL);
        ds.setUsername(databaseUserName);
        ds.setPassword(databasePassword);

        return ds;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
   }

ConcretteDSConfiguration这样的多个类用于构建应用程序上下文。

@Configuration
public class ConcretteDSConfiguration extends GeneralDSConfiguration {

    public ConcretteDSConfiguration() {
        super("jdbc:postgresql://localhost/database", "postgres", "");
    }
}

当我尝试使用new AnnotationConfigApplicationContext(ConcretteDSConfiguration.class)加载应用程序上下文时,将抛出IllegalArgumentException:

Exception in thread "main" java.lang.IllegalArgumentException: @EnableTransactionManagement is not present on importing class 'foo.bar.ConcretteDSConfiguration' as expected
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.context.annotation.AdviceModeImportSelector.selectImports(AdviceModeImportSelector.java:72)
    at org.springframework.context.annotation.ConfigurationClassParser.processImport(ConfigurationClassParser.java:328)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:232)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:139)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:282)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)

它一直是继承的定义,因为如果我将@EnableTransactionManagement注释放到ConcretteDSConfiguration类,一切正常。但我试图找到一种通用方式来编写配置,并且不想将此注释放到每个配置类中。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

它不起作用,因为@EnableTransactionManagement不是@Inherited

无论如何,仅在@Enable...的{​​{1}}上放置任何@Configuration注释就足够了,因此,您可以查看模型并将其拆分为多个配置类并使用它们构建{{ 1}}。

目前尚不清楚如何通过该继承获得良好的解决方案,因为当一个bean覆盖另一个bean时,你最终会遇到问题,因为所有的继承者都会填充相同的bean:AnnotationConfigApplicationContext,{{ 1}}等...

尝试使用针对每个目标上下文的不同dataSource()填充数据库属性,并使用transactionManager()和那些bean填充单个@PropertySource