CategoryBeanMapper
This is a little helper class that can be used to set the attributes of a bean with the properties in a category.
Currently the following types of attributes are supported:
- primitives: char, long, int, float, double
- Strings
This helper class supports two method to configure a bean:
public static void mapBean(Object obj,String categoryName) public static void mapBean(Object obj,String categoryName,String configName)
You need to pass over the instance of your bean and the category name. You can also define the configuration name if you want to get the properties from a different configuration.
Example
First we define a simple Java bean with some attributes:
public class JDBCBeanHelper {
private String URL;
private String DRIVER;
private String PWD;
private String USER;
public JDBCBeanHelper() {
}
public String getURL() {
return URL;
}
public void setURL(String URL) {
this.URL = URL;
}
..... more getter and setter are here
}
In the config.xml we will define a category called "JDBC" like this:
<category name="JDBC"> <property name="URL" value="jdbc:mysql://localhost/iportal"/> <property name="DRIVER" value="org.gjt.mm.mysql.Driver"/> <property name="PWD" value="dice"/> <property name="USER" value="dice"/> </category>
Now to let the CategoryBeanMapper configure our bean we will simply call:
JDBCBeanHelper helper = new JDBCBeanHelper(); CategoryBeanMapper.mapBean(helper,"JDBC","default");
Now you can access the attribute like:
System.out.println("user:"+helper.getUSER());
Please note that the name of the get and set method must match the propery names inside a category. It is also important to match the lower and uppercase letters.