We have developed a Change Watcher .using the CGLIB API. based on the below example
import java.util.List;
public class Teacher {
private String userName;
private String cource;
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCource() {
return cource;
}
public void setCource(String cource) {
this.cource = cource;
}
}
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class ClassFacadeCglib implements MethodInterceptor{
private Object target;
public Object getInstance(Object target) {
this.target = target;
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(this.target.getClass());
// callback method
enhancer.setCallback(this);
// create proxy object
return enhancer.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
if(method.getName().startsWith("set")){
System.out.println(method.getName()+" start");
proxy.invokeSuper(obj, args);
System.out.println(method.getName()+" end..");
}
if(method.getName().startsWith("get")){
System.out.println(method.getName()+" start");
proxy.invokeSuper(obj, args);
System.out.println(method.getName()+" end");
}
return null;
}
}
public class Main {
public static void main(String[] args) {
ClassFacadeCglib cglib=new ClassFacadeCglib();
Teacher teacher=(Teacher)cglib.getInstance(new Teacher());
teacher.setCource("Math");
teacher.getUserName();
}
}
if these classes belong to the same package and bundle the enhancer.create() returns the proxy object to which we can perform the interceptor process on the Setters of the class.
However if seperate the bundle and packages of the interceptor class the enhacer.create() returns null and this blocks us from going with the interceptor process is there a different approach that we need to follow . The idea behind separation of bundle and package is to keep the developer code different from the framework code and restrict the outside world to know about the byte code usage.. To summerize the fact.
Class A and Class B (Teacher and Students Classes) candidates for interceptor present in the Package X (developer package)
Class D is the interceptor class(the CGlib class mentioned above) and Class C is the Framework Class both are present in the framework package.
Package Developer
Class Delegate
{
C c =new C()
Teacher teach =(Teacher) c.write(new Teacher());
}
Package Framework
Class C
{
Public Object write(Object model)
{
ClassFacadeCglib cglib=new ClassFacadeCglib();
Object obj=(Object )cglib.getInstance(model); //calls returns
enhancher.create() as null and thus we cannot perform the interceptor steps
return obj;
}
}
Aucun commentaire:
Enregistrer un commentaire