chaitu chaitu
asked on
how to load properties file only once in enterprise application
This is the class which loads properties file which returns static instance;
**********************************************************************
public class LoadProperties {
private static Properties properties = new Properties();
public static Properties getProperties() {
return properties;
}
public static void setProperties(
Properties properties) {
LoadProperties.properties = properties;
}
}
This is Listener class mentioned in web.xml which can be read when server starts up;
**********************************************************************
public class LoadProperties implements ServletContextListener {
public void contextInitialized(ServletContextEvent arg0) {
//Below properties placed in application server class path in server lib folder with compressed jar;
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("server.properties");
LoadProperties.getProperties().load(inputStream);
//value is printed successfully;
System.out.println("LoadProperties.."+LoadProperties.getProperties().getProperty("type"));
}
}
My requirement is How do we read this property(LoadProperties.ge
my EAR structure is like this;
APP.ear
lib--- jars -- server.properties related jar is placed in the application server class path not in this folder;
ejb.jar
web.war
ASKER
you mean to say i need to again load this properties file in EJB beans as well;
if i do like this ,does this properties will be loaded?
InputStream inputStream = this.getClass().getClassLo ader().get ResourceAs Stream"ser ver.proper ties");
if i do like this ,does this properties will be loaded?
InputStream inputStream = this.getClass().getClassLo
I think you are correct - but you need to have server.properties in the classpath
I think there is a lot of useful information on this subject here (inclufding recommendation to put application-wide properties in ejb-jar.xml)
http://www.velocityreviews.com/forums/t692007-properties-file-in-a-jar-for-ejb-3-0-a.html
I think there is a lot of useful information on this subject here (inclufding recommendation to put application-wide properties in ejb-jar.xml)
http://www.velocityreviews.com/forums/t692007-properties-file-in-a-jar-for-ejb-3-0-a.html
I think this is a good example of how to do it without reading properties many time:
http://javaevangelist.blogspot.com/2008/08/how-to-implement-ejb-30-in-ejb-jarxml.html
and this is the full project from that page to check how it is implemented:
http://enlightenment.bluelotussoftware.com/examples/EJBEnvironmentResourceInjectionExample.zip
http://javaevangelist.blogspot.com/2008/08/how-to-implement-ejb-30-in-ejb-jarxml.html
and this is the full project from that page to check how it is implemented:
http://enlightenment.bluelotussoftware.com/examples/EJBEnvironmentResourceInjectionExample.zip
ASKER
I will do one more excercise.
i will remove Listner class mentioned in web.xml;so i dont read this properties in Servlet;
instead of that i will read the properties in EJB then the LoadProperties instance will be loaded right.
then after that servlet doesn’t find a resource in the WAR, then it should delegate to its parent classloader.
will it work?
i will remove Listner class mentioned in web.xml;so i dont read this properties in Servlet;
instead of that i will read the properties in EJB then the LoadProperties instance will be loaded right.
then after that servlet doesn’t find a resource in the WAR, then it should delegate to its parent classloader.
will it work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)
This question is discussed in this rather old trail below. While the tone of the discussion is pessimistic, there are some suggestions there about using entity bean or JNDI to store such application -wise propertis:
http://www.velocityreviews.com/forums/t139068-get-and-set-attribute-for-ejb-context-storing-objects.html