简体   繁体   中英

Using configurable properties in Java web applications

I have a java based web application deployed on Tomcat 6. I need to make some properties as configurable. Currently i have created a config.properties file and load that file in a static Properties object.

I want to know if there is any other efficient method or framework to use configurable properties in Java web applications?

Another option you might have would be to have one class with all your constants of your projects defined in it. This will provide you with a centralized way in which you can configure your application effectively and efficiently.

That being said however, I think that using the configuration files is the best option since (I do not think) that you would have to recompile your code each time after changing.

EDIT: Seeing some of the comments above, what you could make would be to have a separate table in your database in which you would be able to store all your constants. You can then make this table available to system administrators and other support personnel through a back end web interface.

Try this sample;

This is sample Resource.properties file that place in com.package;

     name=John
     email=john@company.com
     description=John is a Java software developer

And access likes this;

     private static final String PROPERTIES_FILE = "com/package/Resource.properties";

     Properties properties = new Properties();  
     properties.load(this.getClass().getResourceAsStream(PROPERTIES_FILE));  
     String name = props.getProperty("name");  
     String email = props.getProperty("email");  
     String description = props.getProperty("description");

The other framework to use configurable properties is JSF .This sample is the usages of properties in JSF.

The enterprise level answer would be to load your configuration through an integration framework like Spring . If your application is fairly small I wouldn't necessarily recommend it, though.

Loading properties with Spring Framework:

<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-3.0.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:configuration.properties"></property>
    </bean>

    <!-- Here is configutaion for connection pool -->
    <!-- Those ${} properties are from the configuration.properties file -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.user}"/>
        <property name="password" value="${db.pass}"/>
    </bean>

</beans>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM