English 中文(简体)
春瓦底工作
原标题:Ant task for Spring Validation

I need an ANT task to validate spring configuration. I need to find problems at build time before runtime ? For example, In spring context file contains a property a bean, but this bean doesnt have this property. In eclipse, there is a tool Spring Explorer that do this validation.

感谢

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.

Does the parameter type of the setter match the return type of the getter?.

问题回答

An easy way to ensure that your context is valid would be to create a JUnit test, which loads the context. Using the spring-test.jar support classes makes that easy:

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

在网络应用中使用的一个背景是,Junnit不一定能获得同样的资源(例如,JNDI数据来源),因此,如果你在“应用文献目录”中引用以下内容的话:

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

以及您的“申请”将进口“real”应用环境,并重新界定资源:

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

这样,你的单位测试将装上应用环境(即使你在你的单位测试中没有明确测试),而且你可以相信,你的环境是正确的,因为春天自己装上了它。 如果你有错误,那么单位测试就会失败。





相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签