This article is in continuation to Spring PropertySourcesPlaceholderConfigurer Example -1 where I showed you how to make use of Spring PropertySourcesPlaceholderConfigurer to configure properties based on environment.
My scenario is I want to have different URLs for authorizations based on my environment. I have the below environments as of now and for each environment I want to have different atthorization URLs.
In this article, we will explore two more ways. First method is to have different url keys based on environment and the second method is to have one url key but different property files for each environment.
My scenario is I want to have different URLs for authorizations based on my environment. I have the below environments as of now and for each environment I want to have different atthorization URLs.
- test
- prod
In this article, we will explore two more ways. First method is to have different url keys based on environment and the second method is to have one url key but different property files for each environment.
- Add urls for each environment in a properties file. We will have different property key for each url.
- Instead of having different urls for each environment, we can use the same property key but different properties file.
Below is auth.properties with the url key/values:
url_test_env=http://localhost:8280/springdemo/testAuthorize url_pro_env=http://localhost:8280/springdemo/prodAuthorize
Now we will modify the context file's PropertySourcesPlaceholderConfigurer bean, to add location property to load properties from auth.properties:
<beans>
<bean id="springdemoAuthorization" class="springdemo.TestAuthorizationBean">
<property name="url" value="${url_${env}_env}"/>
</bean>
<bean id="propConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="springdemo/auth.properties"/>
</bean>
</beans>
Notice the url property, the key follows a pattern url_${env}_env. Based on the environment, we will inject the ${env} and we will get different url key names.In the test case below, we set the environment, create the context and check whether the authorization bean has got the correct url.
public void testUrlKeys() { String testUrl = "http://localhost:8280/springdemo/testAuthorize"; String prodUrl = "http://localhost:8280/springdemo/prodAuthorize"; System.setProperty("env", "test"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/springdemo/testPropertConfig.xml"); assertEquals(testUrl, ((TestAuthorizationBean) context.getBean("springdemoAuthorization")).getUrl()); System.setProperty("env", "prod"); context.refresh(); assertEquals(prodUrl, ((TestAuthorizationBean) context.getBean("springdemoAuthorization")).getUrl()); }
For example:
testAuth.properties
url_env=http://localhost:8280/springdemo/testAuthorize
prodAuth.properties
url_env=http://localhost:8280/springdemo/prodAuthorize
There is a slight change in context file.
The location path now has a placeholder ${env}. Based on the ${env} value, PropertySourcesPlaceholderConfigurer will load either testAuth.properties or prodAuth.properties. Out test case remains the same.
<beans>
<bean id="springdemoAuthorization" class="springdemo.TestAuthorizationBean">
<property name="url" value="${url_env}"/>
</bean>
<bean id="propConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="springdemo/${env}auth.properties"/>
</bean>
</beans>
No comments:
Post a Comment