Back Forum Reply New

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

Hi everybody,

This setup works fine:Code:
...
lt;context:annotation-config/gt;
lt;context:component-scan base-package=quot;com.cnh.sop.exlo.daoquot; /gt;
lt;context:property-placeholder location=quot;database.propertiesquot; /gt;
lt;bean id=quot;dataSourcequot; class=quot;oracle.jdbc.pool.OracleConnectionPoolDataSourcequot;
destroy-method=quot;closequot;gt;
lt;property name=quot;driverTypequot; value=quot;${driverClass}quot; /gt;
lt;property name=quot;uclquot; value=quot;${jdbcucl}quot; /gt;
lt;property name=quot;userquot; value=quot;${userName}quot; /gt;
lt;property name=quot;passwordquot; value=quot;${password}quot; /gt;
lt;/beangt;
lt;bean id=quot;sessionFactoryquot;
class=quot;org..orm.hibernate3.annotation.AnnotationSessionFactoryBeanquot;gt;
lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot; /gt;
lt;property name=quot;annotatedClassesquot;gt;
lt;listgt;
lt;valuegt;com.cnh.sop.exlo.domain.entities.Measurelt;/valuegt;
lt;valuegt;com.cnh.sop.exlo.domain.entities.MeasureGrouplt;/valuegt;
lt;/listgt;
lt;/propertygt;
lt;property name=quot;hibernatePropertiesquot;gt;
lt;valuegt;
hibernate.dialect=${hibernateDialect}
hibernate.show_sql=${hibernateShowSql}
hibernate.format_sql=${hibernateFormatSql}
hibernate.use_sql_comments=${hibernateUseSqlComments}        lt;/valuegt;
lt;/propertygt;
lt;/beangt;
lt;bean id=quot;transactionManagerquot;
class=quot;org..orm.hibernate3.HibernateTransactionManagerquot;gt;
lt;constructor-arg ref=quot;sessionFactoryquot; /gt;
lt;/beangt;
lt;bean id=quot;exceptionTranslatorquot;
class=quot;org..dao.annotation.PersistenceExceptionTranslationPostProcessorquot; /gt;
lt;/beansgt;
Now I try to define all the beans in @Configuration files. Both files are like this:Code:
//@Configuration
public class ExloDataSourceConfiguraton {

@Value(quot;${user}quot;)
private String user;

@Value(quot;${password}quot;)
private String password;

@Value(quot;${jdbcucl}quot;)
private String ucl;

@Value(quot;${driverClass}quot;)
private String driverName;

@Bean
public DataSource dataSource()throws SQLException{
OracleConnectionPoolDataSource oracleDataSource = new OracleConnectionPoolDataSource();
oracleDataSource.setUser(this.user);
oracleDataSource.setPassword(this.password);
oracleDataSource.setucl(this.ucl);
oracleDataSource.setDriverType(this.driverName);
return oracleDataSource;
}
}
and:Code:
@Configuration
public class ExloHibernateConfiguration extends ExloDataSourceConfiguraton {

@Value(quot;${hibernateDialect}quot;)
private String hibernateDialect;

@Value(quot;${hibernateShowSql}quot;)
private String showSql;

@Value(quot;${hibernateFormatSql}quot;)
private String formatSql;

@Value(quot;${hibernateUseSqlComments}quot;)
private String useSqlComments;

private Properties hibernateProperties(){
Properties properties = new Properties();
properties.setProperty(quot;hibernate.dialectquot;, hibernateDialect);
properties.setProperty(quot;hibernate.show_sqlquot;, showSql);
properties.setProperty(quot;hibernate.format_sqlquot;, formatSql);
properties.setProperty(quot;hibernate.use_sql_commentsquot;, useSqlComments);
return properties;
}

private Classlt;?gt;[] 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..beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-dao.xml]
11-feb-2011 17:50:27 org..context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org..context.support.GenericApplicationContext@2f3adc56: startup date [Fri Feb 11 17:50:27 CET 2011]; root of context hierarchy
11-feb-2011 17:50:27 org..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..core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [database.properties]
11-feb-2011 17:50:27 org..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..beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org..beans.factory.support.DefaultListableBeanFactory@b92a848: defining beans [org..context.annotation.internalConfigurationAnnotationProcessor,org..context.annotation.internalAutowiredAnnotationProcessor,org..context.annotation.internalRequiredAnnotationProcessor,org..context.annotation.internalCommonAnnotationProcessor,org..context.annotation.internalPersistenceAnnotationProcessor,exloDataSourceConfiguraton,exloHibernateConfiguration,measureDAOImpl,org..beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,sessionFactory,transactionManager,exceptionTranslator]; root of factory hierarchy
11-feb-2011 17:50:27 org..test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org..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..test.context.TestContext.getApplicationContext(TestContext.java:308)
...
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: org..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..beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org..beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
...
at org..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..dao.support.PersistenceExceptionTranslationInterceptor.detectPersistenceExceptionTranslators(PersistenceExceptionTranslationInterceptor.java:142)
at org..dao.support.PersistenceExceptionTranslationInterceptor.lt;initgt;(PersistenceExceptionTranslationInterceptor.java:79)
at org..dao.annotation.PersistenceExceptionTranslationAdvisor.lt;initgt;(PersistenceExceptionTranslationAdvisor.java:70)
at org..dao.annotation.PersistenceExceptionTranslationPostProcessor.setBeanFactory(PersistenceExceptionTranslationPostProcessor.java:99)
at org..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
¥
Back Forum Reply New