jdk动态代理

来源:互联网 发布:淘宝卖家信誉等级表 编辑:程序博客网 时间:2024/06/04 18:54

-------------------------------------------------------------Dao类----------------------------------------------------------------------------------------------------------------------

package com.bdqn.dao.impl;


import org.springframework.stereotype.Repository;


import com.bdqn.dao.StudentDao;
import com.bdqn.entity.Student;
@Repository(value="studentDao")
public class StudentDaoImpl implements StudentDao{


public void save(Student student){
System.out.println("this is save");
}

}

------------------------------------------------------JdkProxy  类(代理类)-----------------------------------------------------------------------------------


package com.bdqn.service.impl;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class JdkProxy implements InvocationHandler{
//代理对象
private Object proxyObject;
//使用真实主题对象,创建代理主题对象
public Object createProxyObject(Object object){
proxyObject = object;
/*
* 第一个参数设置代码使用的类加载器,一般采用跟目标类相同的类加载器
* 第二个参数设置代理类实现的接口,跟目标类使用相同的接口
* 第三个参数设置回调对象,当代理对象的方法被调用时,会调用该参数指定对象的invoke方法
*/
return Proxy.newProxyInstance(this.getClass().getClassLoader(),
 this.proxyObject.getClass().getInterfaces(),
 this);
}
//代理类需要处理的业务逻辑
/*
* 参数,
* Object proxy:代理对象
* Method method:访问的方法
* Object[] args 传递的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object value=null;
if(method.getName().equals("login")){
//调用目标对象,并返回目标对象的返回值,如果没有返回值returnvalue此时是null
//此时调用的是login方法,所以返回值为int
value= method.invoke(proxyObject, args);
}
return value;
}
}

------------------------------------------------------service 类-----------------------------------------------------------------------------------

package com.bdqn.service.impl;



import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import com.bdqn.dao.StudentDao;
import com.bdqn.entity.Student;
import com.bdqn.service.StudentService;
@Service(value="studentService")
public class StudentServiceImpl implements StudentService{
@Resource
private StudentDao studentDao;
@Override
public int login(Student student) {
try {
System.out.println("this is Student");
studentDao.save(student);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}


}

------------------------------------------------------Action类(执行)-----------------------------------------------------------------------------------

package com.bdqn.action;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.bdqn.entity.Student;
import com.bdqn.service.StudentService;
import com.bdqn.service.impl.JdkProxy;




public class StudentAction {

public static void main(String[] args) {
ApplicationContext context = new  ClassPathXmlApplicationContext("applicationContext.xml");
StudentService  studentService=(StudentService)context.getBean("studentService");
Student student = new Student();
//创建代理对象
JdkProxy proxy = new JdkProxy();
studentService = (StudentService)proxy.createProxyObject(studentService);
student.setId(1);
student.setName("ssss");
student.setPwd("222");
studentService.login(student);

}
}
0 0
原创粉丝点击