Import resources conditionally into spring application context

Is it possible to import objects in the context of spring only if a certain condition is met?

<!-- import ONLY IF current environment is NOT testing --> <import resource="classpath:context/caching-context.xml" /> 

I am currently doing this by importing a completely different context in my test files

 @ContextConfiguration(locations = { "classpath*:ApplicationContextTesting.xml" }) 

but perhaps there is a more elegant solution not to support two separate application contexts for production and testing.

+4
source share
1 answer

Use Spring Profiles.

 <beans profile="not_test"> <import resource="classpath:context/caching-context.xml" /> </beans> 

More information in the Spring documentation and in the post.

+8
source

Source: https://habr.com/ru/post/1482360/


All Articles