Spring Autowiring无法正常工作

时间:2013-10-04 13:54:00

标签: java spring autowired

希望你能帮助我...... 我有两个项目common-lib和common-dao。 每个项目都有自己的弹簧配置文件。

如果我运行以下测试类,则自动装配工作正常,所有属性都由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());
}

 }

这是我的common-dao项目的spring文件:

<?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>

我的common-lib项目中的spring文件包含在以下行中:

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

我遇到的问题是我的dao项目的这个类。我的创作者的自动装配在这里不起作用:

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;
}
   }

有人可以帮我找到问题吗?

1 个答案:

答案 0 :(得分:5)

你如何期望Spring在它无法控制的对象中自动装配任何东西?

如果希望ScoreMapper个实例为ScoreCreator scoreCreator注入一个Spring bean,ScoreMapper实例本身必须是一个Spring bean,即。由Spring创建和管理。