Maven插件验证Spring配置?

Ick*_*ter 17 validation configuration spring maven-2

有谁知道可以用来验证Spring配置文件的Maven插件?通过验证,我的意思是:

  • 验证所有bean引用构建路径上的类
  • 验证所有bean引用是否引用了有效的bean定义
  • 验证没有孤立的bean存在
  • 其他配置错误我敢肯定我错过了.

我四处搜索,没有想出任何东西.

Maven插件非常适合我的目的,但是任何其他工具(Eclipse插件等)都会受到赞赏.

Jes*_*ebb 9

我们在项目中所做的只是编写一个加载Spring配置的JUnit测试.这会做一些你描述的事情:

  • 验证XML
  • 确保bean可以在类路径上加载类(至少bean不是延迟加载的)

它不会检查是否没有孤儿豆.无论如何,考虑到代码中的任何地方,都没有可靠的方法来执行此操作,您可以直接根据ID查找bean.仅仅因为bean没有被任何其他bean引用并不意味着它没有被使用.事实上,所有Spring配置都至少有一个bean没有被其他bean引用,因为总是必须有层次结构的根.

如果您的bean依赖于数据库等实际服务,并且您不希望在JUnit测试中连接到这些服务,则只需抽象配置以允许测试值.这可以通过PropertyPlaceholderConfigurer之类的东西轻松完成,它允许您为每个环境在单独的配置文件中指定不同的属性,然后由一个bean定义文件引用.

编辑(包括示例代码):
我们这样做的方式是至少有3个不同的弹簧文件......

  • SRC /主/资源/ applicationContext.xml中
  • SRC /主/资源/ beanDefinitions.xml
  • SRC /测试/资源/ testContext.xml

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)

这里有很多事情,让我解释一下......

  • applicationContext.xml的文件是为您的整个应用程序的主春天文件.它包含一个PropertyPlaceHolder bean,允许在我们部署到的不同环境(test vs. prod)之间配置某些属性值.它导入应用程序需要运行的所有主bean.任何不应在测试中使用的bean,如DB bean,或与外部服务/资源通信的其他类,都应在此文件中定义.
  • beanDefinitions.xml文件有所有在它的正常豆不依靠外部的东西.这些bean可以并将引用appContext.xml文件中定义的bean.
  • testContext.xml文件是appContext的测试版本.它需要appContext.xml文件中定义的所有bean的版本,但我们使用模拟库来实例化这些bean.这样就不会使用真正的类,也不存在访问外部资源的风险.此文件也不需要属性占位符bean.

既然我们有一个测试上下文,我们不怕从测试中加载,这里是代码来做...

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并断言.

希望这可以帮助!