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

  • SEARCH
  • Home
  • 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 6809241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:03:46+00:00 2026-05-26T20:03:46+00:00

In spring/junit you can load application context files using @ContextConfiguration such as @ContextConfiguration({classpath:a.xml, classpath:b.xml})

  • 0

In spring/junit you can load application context files using @ContextConfiguration such as

@ContextConfiguration({"classpath:a.xml", "classpath:b.xml"})

I have a requirement where if I see a special annotation on a test class then add another XML context file dynamically. For example:

@ContextConfiguration({"classpath:a.xml", "classpath:b.xml"})
@MySpecialAnnotation 
class MyTest{
...
}

In the above example I would look for @MySpecialAnnotation and add special-context.xml also. What is the best way to do this? I have looked at this for a while and it seems like sub-classing my own ContextLoader which is one of the parameters to @ContextConfiguration is the best approach? Is this correct? Is there a better way to do this?

  • 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-26T20:03:46+00:00Added an answer on May 26, 2026 at 8:03 pm

    It turns out the best solution is to create my own ContextLoader. I did this by extending the abstract one.

    public class MyCustomContextListener extends GenericXmlContextLoader implements ContextLoader {
        @Override
        protected String[] generateDefaultLocations(Class<?> clazz) {
            List<String> locations = newArrayList(super.generateDefaultLocations(clazz));
            locations.addAll(ImmutableList.copyOf(findAdditionalContexts(clazz)));
            return locations.toArray(new String[locations.size()]);
        }
    
        @Override
        protected String[] modifyLocations(Class<?> clazz, String... locations) {
            List<String> files = newArrayList(super.modifyLocations(clazz, locations));
            files.addAll(ImmutableList.copyOf(findAdditionalContexts(clazz)));
            return files.toArray(new String[files.size()]);
        }
    
        private String[] findAdditionalContexts(Class<?> aClass) {
            // Look for annotations and return 'em
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Web application using spring and hibernate and struts (it runs on
I have a Spring MVC application and a problem with JUnit tests combined with
Spring support JUnit quite well on that: With the RunWith and ContextConfiguration annotation, things
I am using Struts2, Spring 2 and Junit 4.5. My main question is, whether
In my project I can successfully test database code. I'm using Spring, Hibernate, HSQLDB,
I have an extremely odd problem in my JUnit tests that I just can't
I have a Spring/JPA/Hibernate application and am trying to get it to pass my
I'm using Spring 3.0.4 and JUnit 4.5. My test classes currently uses Spring's annotation
I have a multi module Spring project that I set up using Maven: my-root
I have 2 Hibernate classes in a Spring-driven Application like these: @Entity public class

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.