我需要一个ANT任务来验证spring配置。我需要在运行前在构建时找到问题?例如,在spring上下文文件中包含一个bean的属性,但是这个bean没有这个属性。在eclipse中,有一个工具Spring Explorer可以完成这个验证。
谢谢,
org.springframework.web.context.ContextLoaderListener failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readController' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'productOperations' of bean class [com.bee.view.json.ReadController]: Bean property 'productOperations' is not writable or has an invalid setter method
。
setter的参数类型是否与getter的返回类型匹配?
确保上下文有效的一种简单方法是创建一个JUnit测试,它将加载上下文。使用spring-test.jar支持类可以简化此操作:
public class MyTest extends AbstractDependencyInjectionSpringContextTests {
// this will be injected by Spring
private QueryDao queryDao;
private MyBusinessObject myBusinessObject;
// ensure that spring will inject the objects to test by name
public MyTest () {
setAutowireMode(AUTOWIRE_BY_NAME);
}
@Override
protected String[] getConfigLocations() {
return new String[] { "applicationContextJUnit.xml" };
}
public void testQueryDao() {
List<SomeData> list = queryDao.findSomeData();
assertNotNull(list);
// etc
}
public void testMyBusinessObject() {
myBusinessObject.someMethod();
}
public void setQueryDao(QueryDao queryDao) {
this.queryDao = queryDao;
}
}
加载在web应用程序中使用的上下文的问题是JUnit不一定能够访问相同的资源(例如JNDI数据源),所以如果你在你的"applicationContext.xml"中有以下内容:
<beans ...>
<bean id="myBusinessObject" class="com.test.MyBusinessObject">
<property name="queryDao" ref="queryDao"/>
</bean>
<bean id="queryDao" class="com.test.QueryDao">
<property name="dataSource" ref="dataSource"/>
</bean>
<jee:jndi-lookup
id="dataSource"
jndi-name="jdbc/mydatasource"
resource-ref="true"
cache="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
</beans>
和"applicationContextJUnit.xml"将导入"真正的"应用程序上下文并重新定义资源:
<beans ...>
<import resource="classpath:applicationContext.xml"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:..."/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
</beans>
这样,你的单元测试将加载应用程序上下文(即使是那些你没有在单元测试中显式测试的),你可以确信你的上下文是正确的,因为Spring自己加载了它。如果出现错误,则单元测试将失败。