Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing resource conditionally in spring application context

Tags:

java

spring

Can I import things in a spring context only if a certain condition is met?

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

currently I am doing that by importing a totally different context in my testcases

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

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

like image 420
MatthiasLaug Avatar asked Feb 17 '23 11:02

MatthiasLaug


1 Answers

Use Spring profiles.

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

More information in Spring documentation and on the blog post.

like image 141
Grzegorz Żur Avatar answered Mar 15 '23 22:03

Grzegorz Żur