Announcement Announcement Module
Collapse
No announcement yet.
@Configuration doesn't work while the same xml setup do work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configuration doesn't work while the same xml setup do work

    Hi everybody,

    This setup works fine:

    Code:
    ...
    	<context:annotation-config/>
    	<context:component-scan base-package="com.cnh.sop.exlo.dao" />
    	<context:property-placeholder location="database.properties" />
    	<bean id="dataSource" class="oracle.jdbc.pool.OracleConnectionPoolDataSource"
    		destroy-method="close">
    		<property name="driverType" value="${driverClass}" />
    		<property name="URL" value="${jdbcUrl}" />
    		<property name="user" value="${userName}" />
    		<property name="password" value="${password}" />
    	</bean>
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>com.cnh.sop.exlo.domain.entities.Measure</value>
    				<value>com.cnh.sop.exlo.domain.entities.MeasureGroup</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<value>
    				hibernate.dialect=${hibernateDialect}
    				hibernate.show_sql=${hibernateShowSql}
    				hibernate.format_sql=${hibernateFormatSql}
    				hibernate.use_sql_comments=${hibernateUseSqlComments}
     		        </value>
    		</property>
    	</bean>
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<constructor-arg ref="sessionFactory" />
    	</bean>
    	<bean id="exceptionTranslator"
    		class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    </beans>
    Now I try to define all the beans in @Configuration files. Both files are like this:

    Code:
    //@Configuration
    public class ExloDataSourceConfiguraton {
    
    	@Value("${user}")
    	private String user;
    	
    	@Value("${password}")
    	private String password;
    	
    	@Value("${jdbcUrl}")
    	private String url;
    	
    	@Value("${driverClass}")
    	private String driverName;
    	
    	@Bean
    	public DataSource dataSource()throws SQLException{
    		OracleConnectionPoolDataSource oracleDataSource = new OracleConnectionPoolDataSource();
    		oracleDataSource.setUser(this.user);
    		oracleDataSource.setPassword(this.password);
    		oracleDataSource.setURL(this.url);
    		oracleDataSource.setDriverType(this.driverName);
    		return oracleDataSource;
    	}
    }
    and:

    Code:
    @Configuration
    public class ExloHibernateConfiguration extends ExloDataSourceConfiguraton {
    
    	@Value("${hibernateDialect}")
    	private String hibernateDialect;
    	
    	@Value("${hibernateShowSql}")
    	private String showSql;
    	
    	@Value("${hibernateFormatSql}")
    	private String formatSql;
    	
    	@Value("${hibernateUseSqlComments}")
    	private String useSqlComments;
    	
    	private Properties hibernateProperties(){
    		Properties properties = new Properties();
    		properties.setProperty("hibernate.dialect", hibernateDialect);
    		properties.setProperty("hibernate.show_sql", showSql);
    		properties.setProperty("hibernate.format_sql", formatSql);
    		properties.setProperty("hibernate.use_sql_comments", useSqlComments);
    		return properties;
    	}
    	
    	private Class<?>[] annotatedClasses = {Measure.class, MeasureGroup.class};
    
    	@Bean
    	public SessionFactory sessionFactory() throws SQLException{
    		AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();
    		annotationSessionFactoryBean.setDataSource(this.dataSource());
    		annotationSessionFactoryBean.setAnnotatedClasses(annotatedClasses);
    		annotationSessionFactoryBean.setHibernateProperties(hibernateProperties());
    		return annotationSessionFactoryBean.getObject();
    	}
    	
    	@Bean
    	public HibernateTransactionManager transactionManager (SessionFactory sessionFactory){
    		HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(sessionFactory);
    		return hibernateTransactionManager;
    	}
    
    	@Bean
    	public PersistenceExceptionTranslationPostProcessor exceptionTranslator(){
    		PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor = new PersistenceExceptionTranslationPostProcessor();
    		persistenceExceptionTranslationPostProcessor.setRepositoryAnnotationType(Repository.class);
    		return persistenceExceptionTranslationPostProcessor;
    	}
    }
    I think they have to define all the same beans. I also tried this without the setRepositoryAnnotationType() method in the last bean.

    I will not post my entire stack trace, but the part I think is relevant. Can anybody tell me what is going wrong and more important, how can I get arround?

    Code:
    ...
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.cnh.sop.exlo.dao.impl.MeasureDAOImplTest
    11-feb-2011 17:50:26 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [spring-dao.xml]
    11-feb-2011 17:50:27 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing [email protected]adc56: startup date [Fri Feb 11 17:50:27 CET 2011]; root of context hierarchy
    11-feb-2011 17:50:27 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
    INFO: Overriding bean definition for bean 'dataSource': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=exloDataSourceConfiguraton; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=null; defined in class path resource [com/cnh/sop/exlo/dao/config/ExloDataSourceConfiguraton.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=exloHibernateConfiguration; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=null; defined in class path resource [com/cnh/sop/exlo/dao/config/ExloHibernateConfiguration.class]]
    11-feb-2011 17:50:27 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
    INFO: Loading properties file from class path resource [database.properties]
    11-feb-2011 17:50:27 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
    INFO: Bean 'exloHibernateConfiguration' of type [class com.cnh.sop.exlo.dao.config.ExloHibernateConfiguration$$EnhancerByCGLIB$$3bed3420] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    11-feb-2011 17:50:27 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b92a848: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,exloDataSourceConfiguraton,exloHibernateConfiguration,measureDAOImpl,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sessionFactory,transactionManager,exceptionTranslator]; root of factory hierarchy
    11-feb-2011 17:50:27 org.springframework.test.context.TestContextManager prepareTestInstance
    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@7bd33a6b] to prepare test instance [com.cnh.sop.exlo.dao.impl.MeasureDAOImplTest@68a0864f]
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    ...
    	at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exceptionTranslator' defined in class path resource [com/cnh/sop/exlo/dao/config/ExloHibernateConfiguration.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    ...
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    	... 28 more
    Caused by: java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:142)
    	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.<init>(PersistenceExceptionTranslationInterceptor.java:79)
    	at org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor.<init>(PersistenceExceptionTranslationAdvisor.java:70)
    	at org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:99)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1439)
    ...
    It is a multi-module Maven-project, all the configuration I have shown comes of the DAO-layer, also the tests are in this layer. With the XML-config it works all just fine, but I connot see where is the difference between the 2 configrations.

    Thanks for your help,

    Benoit
Working...
X