In my product/project development I have a scenario to call the custom code inside core module. I have done the below implementation using Spring and Java Reflection. Provide your suggestion w.r.t further improvements.
Spring Context XML - Configuration:
<bean id="employeeDAOImpl" class="com.spring.employee.dao.EmployeeDAOImpl">
<property name="employeeCustomDAOClassList" ref="employeeCustomDAOClassList"></property>
</bean>
<util:list id="employeeCustomDAOClassList" list-class="java.util.ArrayList">
<value>com.spring.employee.dao.EmployeeCustom1DAOImpl</value>
<value>com.spring.employee.dao.EmployeeCustom2DAOImpl</value>
</util:list>
Core Employe DAO Class:
package com.spring.employee.dao;
import java.lang.reflect.Method;
import java.util.List;
import com.spring.employee.bean.EmployeeVO;
public class EmployeeDAOImpl {
public List<String> employeeCustomDAOClassList;
public EmployeeVO saveEmployee(EmployeeVO employeeVO) {
System.out.println("EmployeeDAOImpl saveEmployee - core business logic executed Sucessfully...");
// TODO implement core business logic to persist employee object.
// invoke custom methods from core based on the configurable approach.
employeeVO = (EmployeeVO) invokeCustomMethods(employeeVO, getEmployeeCustomDAOClassList());
return employeeVO;
}
/**
* This method is used to invoke the custom methods from core methods
*
* @param inputObj
* @param customeClassList
* @return
*/
public Object invokeCustomMethods(Object inputObj, List<String> customeClassList) {
try {
Class[] paramEmployeeVO = new Class[1];
paramEmployeeVO[0] = Object.class;
if (!customeClassList.isEmpty()) {
for (String className : customeClassList) {
Class cls = Class.forName(className);
Object obj = cls.newInstance();
Method method = cls.getDeclaredMethod(Thread.currentThread().getStackTrace()[2].getMethodName(), paramEmployeeVO);
inputObj = method.invoke(obj, inputObj);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return inputObj;
}
/**
* @return the employeeCustomDAOClassList
*/
public List<String> getEmployeeCustomDAOClassList() {
return employeeCustomDAOClassList;
}
/**
* @param employeeCustomDAOClassList
* the employeeCustomDAOClassList to set
*/
public void setEmployeeCustomDAOClassList(List<String> employeeCustomDAOClassList) {
this.employeeCustomDAOClassList = employeeCustomDAOClassList;
}
}
Custom Employee DAO Classes:
public class EmployeeCustom1DAOImpl {
public Object saveEmployee(Object employeeVO) {
System.out.println("Custom 1 EmployeeDAOImpl::saveEmployee - custom business logic executed Successfully...");
return employeeVO;
}
}
public class EmployeeCustom2DAOImpl {
public Object saveEmployee(Object employeeVO) {
System.out.println("Custom 2 EmployeeDAOImpl::saveEmployee - custom business logic executed Successfully...");
return employeeVO;
}
}
Aucun commentaire:
Enregistrer un commentaire