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 类(代理类)-----------------------------------------------------------------------------------
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);
}
}
- 代理模式,JDK动态代理
- Java动态代理--jdk代理
- 代理模式-JDK动态代理
- JDK中的动态代理
- JDK动态代理
- JDK动态代理
- JDK动态代理
- jdk动态代理(暂)
- JDK动态代理
- JDK动态代理实例
- jdk 动态代理
- JDK动态代理
- JDK动态代理说明
- jdk动态代理
- JDKDynamicProxy JDK动态代理
- JDK动态代理实现
- JDK 动态代理
- JDK动态代理
- 想成为Java高手的25个学习目标
- 解决js中setTimeout()函数的兼容问题
- 《动态规划》hdoj acm 3.3.1 简单背包
- MySQL--- DDL、DML、DCL都包括那些语句?
- 《动态规划》hdoj acm 3.3.2 多重背包
- jdk动态代理
- C++之友元类
- 【android】uses-permission和permission详解
- 自定义控件(14)---ViewGroup绘制的Padding、margin注意
- 第26讲 项目4:本月有几天?
- Bootstrap参考文档
- WIN7下安装LINUX虚拟机
- Python模块
- 今天参加面试 上机题,整理一下