Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3482650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:35:01+00:00 2026-05-18T10:35:01+00:00

Does anyone know of a Maven plugin that can be used to validate Spring

  • 0

Does anyone know of a Maven plugin that can be used to validate Spring configuration files? By validation, I mean:

  • Verify all beans reference a class on the build path
  • Verify all bean references refer to a valid bean definition
  • Verify no orphaned beans exist
  • Other configuration mistakes I’m sure I’m missing.

I searched around and didn’t come up with anything.

A Maven plugin would be ideal for my purposes, but any other tools (Eclipse plugin, etc.) would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:35:02+00:00Added an answer on May 18, 2026 at 10:35 am

    What we do on our project is simply write a JUnit test which loads the Spring configuration. This does a few of the things you described like:

    • Validate the XML
    • Ensures beans can be loaded with classes on the classpath (at least beans which aren’t lazy-loaded)

    It does not check that there are no orphan beans. There is no reliable way of doing this anyway considering from anywhere in your code, you can lookup beans directly given their ID. Just because a bean is not referenced by any other beans does not mean it is not used. In fact all Spring configs will have at least one bean which is not referenced by other beans because there always has to be a root to the hierarchy.

    If you have beans which rely on real services like databases or something and you don’t want to connect to these services in a JUnit test, you simply need to abstract the configuration to allow for test values. This can be easily accomplished with something like the PropertyPlaceholderConfigurer which allows you to have different properties specified in separate config files for each environment and then referenced by one beans definition file.

    EDIT (to include sample code):
    The way we do this is have at least 3 different spring files…

    • src/main/resources/applicationContext.xml
    • src/main/resources/beanDefinitions.xml
    • src/test/resources/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>
    

    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>
    

    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>
    

    There are many things going on here, let me explain…

    • The applicationContext.xml file is the main spring file for your whole application. It contains an PropertyPlaceHolder bean to allow certain property values to be configurable between different environments we deploy to (test vs. prod). It imports all of the main beans that the app needs to run. Any beans which should not be used in a test, like DB beans, or other classes which communicate with external services/resources should be definied in this file.
    • The beanDefinitions.xml file has all of your normal beans in it which don’t rely on external things. These beans can and will reference beans defined in the appContext.xml file.
    • The testContext.xml file is the test version of the appContext. It needs versions of all beans defined in the appContext.xml file but we used a mocking library to instantiate these beans. This way the real classes aren’t used and there is no risk of access external resources. This file also doesn’t need the property placeholder bean.

    Now that we have a test context which we aren’t afraid to load from a test, here is the code to do it…

    SpringContextTest.java
    package 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
            }
        }
    }
    

    This may not be the optimal way of doing it; the ApplicationContext class is the recommended way of loading spring contexts. The above might be able to be replaced by:

        @Test
        public void springContextCanLoad() {
            ApplicationContext context = new FileSystemXmlApplicationContext("classpath:testContext.xml");
        }
    

    I believe that one line will accomplish everything you need to verify your spring context is wired correctly. From there, you can load beans and assert like before.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of any existing packages or libraries that can be used to
Does anyone know of a good Command Prompt replacement? I've tried bash/Cygwin, but that
Does anyone know a good Java lib that will hook into SVN so I
Does anyone know where online copies of the old The Perl Journal articles can
Does anyone know how I can, in platform-independent C++ code prevent an object from
Does anyone know if there is an Ice grid plugin for Maven2 ? Currently
Does anyone know of a php class or library that helps deal with dates
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone know how to transform a enum value to a human readable value?
Does anyone know of a FOSS Python lib for generating Identicons ? I've looked,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.