希望你能帮助我...我有两个项目 common-lib 和 common-dao。每个项目都有自己的 spring 配置文件。
如果我运行以下测试类,则自动装配工作正常,并且所有属性都是由 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 项目的这个类中。我的 Creator 的自动装配在这里不起作用:
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;
}
}
有人可以帮我找到问题吗?