Announcement Announcement Module
Collapse
No announcement yet.
@Configuration failing with @EnableTransactionManagement Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configuration failing with @EnableTransactionManagement

    Hi,
    When I am using @Configuration with @EnableTransactionManagement, then my Integ test: DBSuiteOperatorTest is unable to autowire my beans. If I do not use @EnableTransactionManagement, then autowiring works fine. I also tried replacing @EnableTransactionManagement with @ImportResource("spring/tx-context.xml"), but again the autowiring fails. Please advise.

    The code is listed below:
    ---------------------------
    @Configuration
    @EnableTransactionManagement
    public class ConfigSuiteOperator {

    @Bean
    public DBSuiteOperator getDBSuiteOperator() {
    DBSuiteOperator operator = new DBSuiteOperator();
    operator.setDataSource(getDataSource());
    return operator;
    }

    @Bean
    public DataSource getDataSource() {
    DataSource ds = new DataSource();
    ds.setDriverClassName("oracle.jdbc.OracleDriver");
    ds.setUrl("jdbc:oracle:thin:@server:1521:FDR");
    ds.setUsername("user");
    ds.setPassword("xxx");
    ds.setMaxIdle(5);
    ds.setMaxWait(-1);
    ds.setMaxActive(10);

    return ds;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager getDataSourceTransactionManager() {
    DataSourceTransactionManager tm = new DataSourceTransactionManager();
    tm.setDataSource(getDataSource());
    return tm;
    }
    }

    ----------------------------

    @ContextConfiguration(classes = ConfigSuiteOperator.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class DBSuiteOperatorTest {

    @Autowired
    DBSuiteOperator operator;

    @Test
    public void assertDataInsertedCorrectly() {
    operator.persistSuite(SuiteVOBuilder.buildSuiteVO( ));
    }
    }
    -----------------------------
    public class DBSuiteOperator extends NamedParameterJdbcDaoSupport
    implements SuiteOperator {

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public String persistSuite(final SuiteVO suiteVO) {
    String insertQuery = "insert into store " +
    "(suite_id) " +
    "values (:suite_id)";
    List<ScenarioVO> scenarios = suiteVO.getScenarios();
    getNamedParameterJdbcTemplate()
    .batchUpdate(insertQuery, getBatchRowValues(suiteVO.getMachineId(), scenarios));
    return null;
    }

    private Map<String, ?>[] getBatchRowValues(String machineId, List<ScenarioVO> scenarios) {
    @SuppressWarnings("unchecked")
    Map<String, ?>[] batchValues = new HashMap[scenarios.size()];
    for (int i = 0; i < scenarios.size(); i++) {
    batchValues[i] = getEachRowValues(machineId, scenarios.get(i));
    }

    return batchValues;
    }

    private Map<String, ?> getEachRowValues(String machineId, ScenarioVO scenarioVO) {
    Map<String, Object> rowValues = new HashMap<String, Object>();
    rowValues.put("suite_id", 1);
    return rowValues;
    }
    }

    -------------------------
    Exception:
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'com.cobalt.leadverify.operator.util.DBSuiteOperat orTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: com.cobalt.leadverify.operator.suite.DBSuiteOperat or com.cobalt.leadverify.operator.util.DBSuiteOperato rTest.operator; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [com.cobalt.leadverify.operator.suite.DBSuiteOperat or] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 288)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireBeanProperties( AbstractAutowireCapableBeanFactory.java:379)
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.injectDependencies (DependencyInjectionTestExecutionListener.java:110 )
    at org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener.prepareTestInstanc e(DependencyInjectionTestExecutionListener.java:75 )
    at org.springframework.test.context.TestContextManage r.prepareTestInstance(TestContextManager.java:313)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.createTest(SpringJUnit4ClassRunner.j ava:211)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner$1.runReflectiveCall(SpringJUnit4Clas sRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.methodBlock(SpringJUnit4ClassRunner. java:284)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:231)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.runChild(SpringJUnit4ClassRunner.jav a:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:229)
    at org.springframework.test.context.junit4.statements .RunBeforeTestClassCallbacks.evaluate(RunBeforeTes tClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements .RunAfterTestClassCallbacks.evaluate(RunAfterTestC lassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:309)
    at org.springframework.test.context.junit4.SpringJUni t4ClassRunner.run(SpringJUnit4ClassRunner.java:174 )
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: com.cobalt.leadverify.operator.suite.DBSuiteOperat or com.cobalt.leadverify.operator.util.DBSuiteOperato rTest.operator; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [com.cobalt.leadverify.operator.suite.DBSuiteOperat or] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:514)
    at org.springframework.beans.factory.annotation.Injec tionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 285)
    ... 26 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [com.cobalt.leadverify.operator.suite.DBSuiteOperat or] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.raiseNoSuchBeanDefinitionExcept ion(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.doResolveDependency(DefaultList ableBeanFactory.java:818)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.resolveDependency(DefaultListab leBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:486)
    ... 28 more

    Thanks.

  • #2
    By the way, I am using spring 3.2

    Comment

    Working...
    X