I am having a property file with 68 parameters and i had declared 68 properties in bean and i am able get these values using application context in a class but to get all these values i need to write 68 getmethods and then need to assign a class variable instead of doing it is there any way to get all these values in couple of steps. Please refer my code.
public class ConfigUtil {
private String jdbcUrl;
private String dbUserName;
private String dbPassword;
private String serverPort;
private String clientPort;
//gettters and setters
}
My xml is
<bean id="configUtil" class="com.cmc.pnc.rdt.util.ConfigUtil">
<property name="jdbcUrl" value="${DATABASECONNECTIONSTRING}"></property>
<property name="dbUserName" value="${DATABASEUSERID}"></property>
<property name="dbPassword" value="${DATABASEPASSWD}"></property>
<property name="serverPort" value="${SERVERPORT}"></property>
<property name="clientPort" value="${CLIENTPORT}"></property>
</bean>
My Java code to get those values is
ApplicationContext appctx = new ClassPathXmlApplicationContext("rdt-server- context.xml");
ConfigUtil configUtil =(ConfigUtil)appctx.getBean("configUtil");
RDTServerMain.DATABASECONNECTIONSTRING = configUtil.getJdbcUrl();
RDTServerMain.DATABASEUSERID=configUtil.getDbUserName();
RDTServerMain.DATABASEPASSWD=configUtil.getDbPassword();
RDTServerMain.SERVERPORT=Integer.parseInt(configUtil.getServerPort());
RDTServerMain.CLIENTPORT=Integer.parseInt(configUtil.getClientPort());
My RDTServerMain class is
public static String DATABASECONNECTIONSTRING = "";
public static String DATABASEUSERID = "";
public static String DATABASEPASSWD = "";
public static int SERVERPORT;
public static int CLIENTPORT;
I tried with BeanInfo and PropertyDescriptors but still facing issues. So could any one suggest this without writing multiple lines to get all the values how can we achieve using Java Reflections or Spring ReflectionUtils I am having existing code if i pass a key and value it will be very easy to get all remaining methods .(Because already i have code to set values for different approach i want to try with new approach )
private static void loadConfigFile(String key, String value){
if (key.equalsIgnoreCase("DATABASECONNECTIONSTRING")) {
RDTServerMain.DATABASECONNECTIONSTRING = value;
} else if (key.equalsIgnoreCase("DATABASEUSERID")) {
RDTServerMain.DATABASEUSERID = value;
} else if (key.equalsIgnoreCase("DATABASEPASSWD")) {
RDTServerMain.DATABASEPASSWD = value;
} else if (key.equalsIgnoreCase("SERVERPORT")) {
RDTServerMain.SERVERPORT = Integer.parseInt(value);
} else if (key.equalsIgnoreCase("CLIENTPORT")) {
RDTServerMain.CLIENTPORT = Integer.parseInt(value);
Aucun commentaire:
Enregistrer un commentaire