In a J2EE application, I would like to use a number of Java objects having properties predefined in configuration text files. These, I thought, might well be implemented as JavaBeans. Rod Johnson, creator of the Spring framework, advocates this strategy.
Do you know of a simple example showing the use of a JavaBean and its Property Descriptor file? I would like to learn from an example how the bean knows where its property descriptor file is. How does a bean know to load its properties from the descriptor? Do descriptor files reside in certain directory locations by default? etc.
I do not know of any simple examples off hand. The area you want to look in is the Java Reflection API (java.lang.reflect). For an excellent resource on this, check out Java Reflection in Action by Ira R. Forman and Nate Forman; Manning Publishing. Also look at the ClassLoader class. Using the Reflection API and the ClassLoader, you can do what you are looking to do. (You can alos look at the ResourceBundle class. Take a look at the JavaWorld article Smartly load your properties for some good information on loading of Properties files.
To answer a couple of your questions:
How does a bean know to load its properties from the descriptor?
I do not know if there is a standard answer to this, but I would simply say that you can write a bean in such a way that it looks for a descriptor file, and if it finds one, it uses it to populate itself.
Do descriptor files reside in certain directory locations by default?
Again, this is a design choice. Probably the easiest solution is to simply have the descriptor file reside on the CLASSPATH. But you could certainly have it reside in a directory that is on the CLASSPATH (the next easiest solution), or in a specific directory on the system (not a very portable solution).
Below is a very simple example of a possible way of doing this. This example makes many assumptions. For example it assumes that all your properties are Strings. There are ways of handling other types. Again, the above reference book on the Reflection API explains such. If I was doing this for a production application, I would probably use an XML file rather then a properties file. As a disclaimer, this is a simple example to help get you started, and was written out quickly, so there is likely much room for improvement. This was written for Java 1.5. If you are using an older version, you will need to make a few modifications as discussed in some of the comments.
After playing around with this example, and reading the Reflection API book, download the source code to Spring and look at how they are going it. You can also look at the Apache Jakarta Commons Digester and BeanUtils APIs as they also use reflection.
Good Luck and Happy Coding
MyBean.properties (Must be placed on the CLASSPATH)
[ October 29, 2006: Message edited by: Mark Vedder ]
Joined: Apr 08, 2003
Mark, thanks for this info. This, like many other questions I have regarding software development, concerns what has--and what has not--already been done for the developer by libraries, frameworks and platforms (J2EE, etc.). What must be done by the developer? What ought best to be done by the existing framework(s)?
I have used reflection before and can use it again. But my real question is, who should implement the property loading functionality? I, the developer, or some helper class in the Java framework?
In the case of javabeans, you are saying, are you not, that I should implement my own strategy of loading properties. You are saying that there are no Java classes (BeanDescriptor, PropertyDescriptor, BeanInfo, open sources commons framework classes) that already do this. This is not a rhetorical question, since I am not exactly sure what these classes do. But I ask in case it would be better to use existing framework classes rather than property loading strategies of my own design.
If there are no such helper classes I will waste no time in rolling my own beans!
Joined: Apr 08, 2003
In the meantime I will poke around in the Jakarta Beanutils package as you suggested. And the Spring framework is another place to look.
What must be done by the developer? What ought best to be done by the existing framework(s)? ... But my real question is, who should implement the property loading functionality? I, the developer, or some helper class in the Java framework? ... In the case of javabeans, you are saying, are you not, that I should implement my own strategy of loading properties. You are saying that there are no Java classes (BeanDescriptor, PropertyDescriptor, BeanInfo, open sources commons framework classes) that already do this. ...
I am sorry to say that I have not done enough work with JavaBeans, at least in terms of auto population, to answer any of these questions authoritatively. So I will need to leave the answers to these questions to someone else who may know the answers. If you do not get any replies here in the beginner forum, you may want to ask your question in the EJB and Other J2EE Technologies forum. If you happen to come across the answer in your own research, please post the results as I am curious myself.
Joined: Apr 08, 2003
The short answer is that you have to write your own. The Spring Framework includes its own classes to intialize bean properties, which would seem to mean that the creators of the Framework did not or chose not to rely on Jakarta or other preexisting beanhandling frameworks.
Note, by the way, that all of this pertains to Javabeans. It has nothing to do with EJBs.
The Spring bean-handling architecture seems to run something like this:
1. you define beans in configuration files. The config file will either application-context.xml in your J2EE web container (tomcat), or a spring configuration file, which could be called anything but often "beans.xml" which sits somewhere in your web container at a location of your choosing. The config file will include a definition for each bean: