Announcement Announcement Module
Collapse
No announcement yet.
Definig properties files outside you webapp in spring . Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Definig properties files outside you webapp in spring .

    Problem Description : I am creating webapp which is a .war and deploying it on JBOSS . I am using IOC feature of spring , and my spring XML looks like

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location">
    <value>classpath:META-INF/prop.properties</value>
    </property>
    </bean>

    <bean id="XYZLogger" class="XYZ.Abc" >
    <constructor-arg index="0" value="xyz"/>
    <property name="hostName" value="${loggerName}" />
    </bean>
    </beans>

    By using above approach , spring will look for prop.properties in classpath . But if i want to make it configurable i.e i want this path to be specified in JBOSS and spring should look for the properties file at that location only .

    below is the solution for it

    package a.b.c;
    import java.io.File;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.config.PropertyP laceholderConfigurer;
    import org.springframework.core.io.Resource;

    public class MyPropertyPlaceholderConfigurer extends
    PropertyPlaceholderConfigurer {

    public static String configPath = "BLANK";
    public final Logger propertyPlaceholderConfigurerLogger = Logger
    .getLogger(APCPropertyPlaceholderConfigurer.class) ;

    @Override
    public void setLocation(Resource location) {
    Resource[] locations = new Resource[] { location };
    Resource[] loc = modifyLocations(locations);
    super.setLocations(loc);
    }

    @Override
    public void setLocations(Resource[] location) {
    Resource[] locations = location;
    Resource[] loc = modifyLocations(locations);
    super.setLocations(loc);
    }

    private Resource[] modifyLocations(Resource[] locations) {
    if (!configPath.equals("BLANK")) {
    Resource[] myLocation = new Resource[locations.length];
    int i = 0;
    for (Resource res : locations) {
    try {
    if (res instanceof org.springframework.core.io.FileSystemResource) {
    propertyPlaceholderConfigurerLogger
    .debug("FilePath defined in spring Configuration , will not modify this path");
    myLocation[i++] = res;
    }
    else if (res instanceof org.springframework.core.io.ClassPathResource) {
    org.springframework.core.io.ClassPathResource res1 = (org.springframework.core.io.ClassPathResource) res;
    if (!configPath.endsWith(File.separator))
    configPath += File.separator;
    org.springframework.core.io.FileSystemResource myClass = new org.springframework.core.io.FileSystemResource(
    configPath + res1.getPath());
    myLocation[i++] = myClass;
    }
    else
    myLocation[i++] = res;
    }
    catch (Exception e) {
    propertyPlaceholderConfigurerLogger.error(e.getMes sage());
    }
    }
    return myLocation;
    }
    else {
    propertyPlaceholderConfigurerLogger
    .error("Config Location for APC Config files not found");
    return locations;
    }
    }

    }

    You just need to set the config path which can be done from servlet or contextEventListeners for example

    package a.b.c;
    import javax.servlet.ServletContextEvent;
    import org.springframework.web.context.ContextLoaderListe ner;

    public class MyContextListener extends ContextLoaderListener {

    @Override
    public void contextInitialized(ServletContextEvent contextEvent) {
    super.contextInitialized(contextEvent);
    String configpath = null;
    try {
    configpath = contextEvent.getServletContext()
    .getInitParameter("PropertiesLocation");
    if (configpath != null)
    MyPropertyPlaceholderConfigurer.configPath = configpath;
    }
    catch (Exception e) {
    System.out.println("Config path not Configured");
    }

    }
    }


    and defining following properties in your context.xml

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Parameter name="PropertiesLocation" value="C:\Prop_Location" override="false"/>

    Use MyContextListener in your web.xml

    <listener-class>a.b.c.MyContextListener</listener-class>

    Modify Spring's bean injection xml file to use MyPropertyPlaceholderConfigurer

    <bean class="a.b.c.MyPropertyPlaceholderConfigurer">
    <property name="location">
    <value>classpath:META-INF/prop.properties</value>
    </property>
    </bean>

    <bean id="XYZLogger" class="XYZ.Abc" >
    <constructor-arg index="0" value="xyz"/>
    <property name="hostName" value="${loggerName}" />
    </bean>
    </beans>

  • #2
    1) When posting code use [ code][/code ] tags.
    2) It is a solution to a problem which is already solved.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      1) When posting code use [ code][/code ] tags.
      2) It is a solution to a problem which is already solved.
      thanks for the suggestion ,this was my first post and i am not use to the format .
      I posted the solution so as to share with other developers around the world , can't we post solution and problems together .

      Comment

      Working...
      X