Ick*_*ter 17 validation configuration spring maven-2
有谁知道可以用来验证Spring配置文件的Maven插件?通过验证,我的意思是:
我四处搜索,没有想出任何东西.
Maven插件非常适合我的目的,但是任何其他工具(Eclipse插件等)都会受到赞赏.
我们在项目中所做的只是编写一个加载Spring配置的JUnit测试.这会做一些你描述的事情:
它不会检查是否没有孤儿豆.无论如何,考虑到代码中的任何地方,都没有可靠的方法来执行此操作,您可以直接根据ID查找bean.仅仅因为bean没有被任何其他bean引用并不意味着它没有被使用.事实上,所有Spring配置都至少有一个bean没有被其他bean引用,因为总是必须有层次结构的根.
如果您的bean依赖于数据库等实际服务,并且您不希望在JUnit测试中连接到这些服务,则只需抽象配置以允许测试值.这可以通过PropertyPlaceholderConfigurer之类的东西轻松完成,它允许您为每个环境在单独的配置文件中指定不同的属性,然后由一个bean定义文件引用.
编辑(包括示例代码):
我们这样做的方式是至少有3个不同的弹簧文件......
applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:beanDefinitions.xml"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:path/environment.properties" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}" />
...
</bean>
... <!-- more beans which shouldn't be loaded in a test go here -->
</beans>
Run Code Online (Sandbox Code Playgroud)
beanDefinitions.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myBean" class="com.example.MyClass">
...
</bean>
<bean id="myRepo" class="com.example.MyRepository">
<property name="dataSource" ref="dataSource"/>
...
</bean>
... <!-- more beans which should be loaded in a test -->
</beans>
Run Code Online (Sandbox Code Playgroud)
testContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:beanDefinitions.xml"/>
<bean id="dataSource" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.jdbc.datasource.DriverManagerDataSource"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
这里有很多事情,让我解释一下......
既然我们有一个测试上下文,我们不怕从测试中加载,这里是代码来做...
SpringContextTest.java 包com.example;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class SpringContextTest {
@Test
public void springContextCanLoad() {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("testContext.xml"));
for (String beanName : factory.getBeanDefinitionNames()) {
Object bean = factory.getBean(beanName);
// assert anything you want
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可能不是最佳方式; ApplicationContext类是加载spring上下文的推荐方法.以上可能可以替换为:
@Test
public void springContextCanLoad() {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:testContext.xml");
}
Run Code Online (Sandbox Code Playgroud)
我相信一行将完成验证弹簧环境正确接线所需的一切.从那里,您可以像以前一样加载bean并断言.
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
3753 次 |
| 最近记录: |