In the previous part we learned about the Spring Configurator. In this part we learn how to create properties for different environments (or running modes) and how to use them.
First we add a simple String property with the name text to our Sample class:
package com.mrhaki.spring.mavenapp;
import org.apache.cocoon.configuration.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sample {
private Settings settings;
private String text;
@Autowired
public void setSettings(Settings settings) {
this.settings = settings;
}
public void setText(String name) {
this.text = name;
}
@Override
public String toString() {
return "Running in [" + settings.getRunningMode() + "] mode. "
+ "Text = [" + text + "].";
}
}
Notice how we changed the toString method so we can see the value for the text property. Next we add the bean definition for the Sample class in our applicationContext.xml. Therefore we add the following lines after <configurator:settings/>:
<bean name="sample" class="com.mrhaki.spring.mavenapp.Sample">
<property name="text" value="${sample.text}"/>
</bean>
At line 2 we assign the text property a variable value, which is not set yet. The value will be set if we run the application and is read from a property file. We can now use the Spring Configurator to define a different value for sample.text for every running mode we want to support. For this example we assume a dev and test running mode. If we read the Spring Configuration manual we notice we can place the property files at many locations. This is what makes the Spring Configurator so flexible and useful.
First we create in the src/main/resources directory the following directories: META-INF/cocoon/properties/dev and META-INF/cocoon/properties/test. We create a sample.properties file in the src/main/resources/META-INF/cocoon/properties/dev directory. In the file we define:
sample.text=We are developing!
We save the file and if we run mvn test -Dorg.apache.cocoon.mode=dev we get the following output:
Running in [dev] mode. Text = [We are developing!].
The Spring Configurator has replaced our variable sample.text with the value from the property file. We also create a sample.properties file in the src/main/resources/META-INF/cocoon/properties/test directory and give a different value for the property:
sample.text=Ready for testing!
We run the test again, but now use the test running mode:mvn test -Dorg.apache.cocoon.mode=test. And we get this output:
Running in [test] mode. Text = [Ready for testing!]
Finally we create a sample.properties in the src/main/resources/META-INF/cocoon/properties directory with a default value, which is used by the default running mode (prod):
sample.text=We are alive!
We run mvn test and we see in the output:
Running in [prod] mode. Text = [We are alive!].
So in this part we have learned how to use property files for different running modes to replace variables in the Spring configuration files. In the next part we learn how override properties of beans defined in Spring configuration files.
4 comments:
Hi Haki,
I am getting below exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cocoon.configuration.Settings': Invocation of init method failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.apache.cocoon.configuration.Settings' is defined
Hi RamKri,
did you include the namespace part, xmlns:configurator="http://cocoon.apache.org/schema/configurator" and the rest, in your Spring configuration file?
Hi RamKri,
if you use an hierarchy of Spring configuration files, you must include the <configurator:settings/> in parent configuration files as well. So for example in a default Spring WebMVC you can define <configurator:settings/> in the dispatch-servlet.xml file, but you must also define it in the applicationContext.xml file.
Hi Haki,
I included namespace part. I dont have any specific applicationContext.xml. We segregated spring xml files to applicatioinContext-hibernate.xml , applicatioinContext-service.xml.....
I added below in my dispatch-servlet.xml file with namespace stuff
< context:component-scan base-package="com.cmc.wolongapp.properties" / >
< configurator:settings / >
< bean id="spproperties" class="com.cmc.wolongapp.properties.SPProperties">
< property name="spPaymentURL" value="${test.text}" / >
< / bean >
Post a Comment