|
WebPropertyConfigurer
Configleon is very simple to set up and use. At a high level this is how it works.
Now I will walk through each step in more detail. In this example we will assume that the application is being deployed to the test web server. Note: Configleon also works perfectly with Spring applications as well as integration tests. This page is only concentrating on setting up a web application. Define JVM variableYou start by defining a JVM variable that relates to a specific environment. The syntax looks like this. -DconfigLocation=test Then Configleon will use this variable to load all the property attributes related to the test environment. The location it expects to find the property files is in your WEB-INF directory with a subdirectory of config/test. When you fire up the web server with info logging enabled you should see the following message to verify that it is loading up the expected environment. The configLocation JVM variable is test Create The Folder And Property FilesFor a web application Configleon expects your files to be in the WEB-INF directory with a subdirectory of config followed by a subdirectory for the location. In this example the directory structure would be WEB-INF/config/test. So now we need to add the property files. The first property file that Configleon will look for is config-global.properties in the config subdirectory. This is always the case and goes across all the environments. The next property file that Configleon will look for is config-test-global.properties in the config/test subdirectory. This is the global file for the test environment. The last property that Configleon will look for is config-test-jmesa.properties in the config/test subdirectory. Visually it looks like this.
You can define all these files, or just one of them. The real power though is when you define all three. What Configleon does is load each one in order. Not only does it make it easy to define all the common property attributes in a global file, but you can override any attribute in the subsequent files. When you fire up the web server with info logging enabled you should see the following messages to verify the property files that Configleon is trying to load. The real path is /home/jeff/workspace/jmesaWeb/build/jmesa/ The context is jmesa Trying to add resource WEB-INF/config/config-global.properties Adding resource WEB-INF/config/config-global.properties Trying to add resource WEB-INF/config/test/config-test-global.properties Adding resource WEB-INF/config/test/config-test-global.properties Trying to add resource WEB-INF/config/test/config-test-jmesa.properties Adding resource WEB-INF/config/test/config-test-jmesa.properties The one file that I kind of glossed over is config-test-jmesa.properties. You are probably wondering what the 'jmesa' part means? In this example 'jmesa' is the server context for the web application. The context is derived by using the real path to the web application, and then picking off the last part of the path as the server context. This also means that you can have multiple applications deployed to the same web application server and Configleon will always load up the correct property files. Note: you have to pay particular attention to the context specific property file. In your local environment (in development) the place where your application is deployed will vary depending on the IDE that you use. Just pay attention to the logging output when the application loads to see where it is looking for the files. For instance, in my local environment you can see (image above) that the context is called web (config-local-web.properties). The reason is because I use Netbeans and that is the folder that the application gets deployed to when I run it in the IDE. Another strategy that works well when running in your local environment is just define the config-local-global.properties file and then it does not matter where your IDE deploys the application to. Define The Property AttributesThe next thing to do is define the property attributes. In this example we are defining the same path and connection attributes. The config-global.properties has these attributes. imagePath=http://mycompany.com/images connection.driver_class=oracle.jdbc.driver.OracleDriver connection.username=jmesa connection.password=password The config-test-global.properties has these attributes. connection.url=jdbc:oracle:thin:@host.mycompany.com:1521:test The config-test-jmesa.properties has these attributes. imagePath=http://test.mycompany.com/images sitePath=http://test.mycompany.com/jmesa When you fire up the web server with debug logging enabled you should see the following messages to see what property attributes are used. property: connection.driver_class --> oracle.jdbc.driver.OracleDriver property: connection.password --> password property: connection.url --> jdbc:oracle:thin:@host.mycompany.com:1521:test property: connection.username --> username property: imagePath --> http://test.mycompany.com/images property: sitePath --> http://test.mycompany.com:8080/jmesa If you look closely you will see that the global imagePath was overridden at the jmesa context level. Also, the connection.url was defined at the test global level. Although this is a very simple example, you can see the cascade power of Configleon. Update The applicationContext.xmlConfigleon is actually a simple application and works by extending Spring's PropertyPlaceholderConfigurer class. In fact you can set any PropertyPlaceholderConfigurer class methods that you want. Just be sure to define the Configleon configurer bean in the applicationContext.xml in place of the Spring configurer bean. <bean class="com.configleon.configurer.WebPropertyConfigurer">
<property name="propertyResources">
<bean class="com.configleon.resource.WebPropertyResources"/>
</property>
</bean>Additional FeaturesIn addition to using property attributes for injecting configuration into Spring beans there are two other ways that you can use the attributes that you define when using Configleon. One is by getting straight at the attributes with the static Config object. The other is by adding attributes to the web application scope. The Static Config ObjectAlthough accessing a property attribute directly with a static object can signal a design flaw in your application, sometimes it is the best way to get at the configuration that you need. String sitePath = Config.getProperty("sitePath");Web Application ScopeMany times when developing web applications it is convenient to be able to add property attributes to the web application scope so that you can get things from the view. To put an attribute in the application scope just add it to the applicationAttributes property. <bean class="com.configleon.configurer.WebPropertyConfigurer">
<property name="propertyResources">
<bean class="com.configleon.resource.WebPropertyResources"/>
</property>
<property name="applicationAttributes">
<list>
<value>sitePath</value>
</list>
</property>
</bean>Now you have access to the attribute from the view. <html>
<head>
<title>JMesa</title>
</head>
<body>
${sitePath}
</body>
</html>
|