nano_nano
nano_nano

Reputation: 12523

Spring Autowiring not working

Hope you could help me... I have two projects common-lib and common-dao. Each project has its own spring configuration file.

If I run the following testclass the autowiring works fine and all attributes are generated by spring.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/cmn-dao-spring.xml"})
public class ScoreDaoTest extends TestCase {

@Autowired
private ScoreDao mScoreDao;

@Autowired
private ScoreCreator mScoreCreator;

@Autowired
private QuestionCreator mQuestionCreator;

@Override
protected void setUp() throws Exception {
    super.setUp();
}

@Test
public void testLoadAllScore() throws Exception {
    List<Score> lAllScore = mScoreDao.loadAllScore(0, 0);
    Assert.assertTrue(lAllScore.isEmpty());
}

@Test
public void testSaveScore() throws Exception {
    Question question = mQuestionCreator.createQuestion(49954854L, new Date(), "Wer bist Du?", "Jörg", "Anja", "Stefan", "Willi", 3, true, false, 1, "DE", "DE_QZ");
    Assert.assertNotNull(question);
    mScoreDao.saveScore(mScoreCreator.createScore(-1L, null, "Stefan", 1033, 27, "Wuhuuuu", question));
    List<Score> lAllScore = mScoreDao.loadAllScore(0, 1);
    Assert.assertFalse(lAllScore.isEmpty());
}

 }

that is my spring file for the common-dao project:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://   www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<tx:annotation-driven />
<context:annotation-config />

<import resource="cmn-lib-spring.xml" />

<!-- DATABASE CONFIGURATION -->

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>database.properties</value>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- BEAN DEFINITIONS -->

<bean id="scoreDao" class="de.bc.qz.dao.ScoreDao" autowire="byName">
    <property name="dataSource" ref="dataSource" />
</bean>

    </beans>

the spring file from my common-lib project is included in this line:

<import resource="cmn-lib-spring.xml" />

The problem I have is inside this class of my dao project. The autowiring for my Creator is not working here:

public class ScoreMapper implements RowMapper<Score> {

private String suffix = "";

@Autowired
private ScoreCreator mScoreCreator;

public ScoreMapper(String pSuffix) {
    suffix = pSuffix;
}

public Score mapRow(ResultSet rs, int rowNum) throws SQLException {
    Score lScore = mScoreCreator.createScore(rs.getLong(suffix+"id"),
            rs.getDate(suffix+"stempel"), rs.getString(suffix+"username"),
            rs.getInt(suffix+"points"), rs.getInt(suffix+"level"),
            rs.getString(suffix+"comment"),
            new QuestionMapper("q.").mapRow(rs, rowNum));
    return lScore;
}
   }

Could someone help me finding the problem?

Upvotes: 0

Views: 2955

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

How do you expect Spring to autowire anything in objects it doesn't control?

If you want ScoreMapper instances to have ScoreCreator scoreCreator be injected with a Spring bean, the ScoreMapper instance itself must be a Spring bean, ie. created and managed by Spring.

Upvotes: 5

Related Questions