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.
To answer a couple of your questions:
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.
How does a bean know to load its properties from the descriptor?
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).
Do descriptor files reside in certain directory locations by default?
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 ]
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!
Originally posted by Benjamin Weaver:
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.
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:
<...another bean definition would go here...>
</another bean definition>
2. you get initialized beans in your Java code via certain Spring Framework classes:
Resource resource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Person person = (Person) factory.getBean("person");