Spring动态代理对象
来源:互联网 发布:秋冬用水乳 知乎 编辑:程序博客网 时间:2024/06/08 12:43
/*****************************************************************************************************/
16:00 2017/10/19 动态AOP代理
/*****************************************************************************************************/
·多个类的话静态切面编程较为麻烦,所以使用动态切面编程。
·动态代理步骤:
(1)、概念:概念上来讲,通过代理对象,创建需要的业务对象,然后通过代理对象,进行各种需求的处理。
(2)、创建的过程(创建代理对象):
<1>、写一个类实现invocationHandler接口。
<2>、在这个类里面创建代理对象(只是声明)。
<3>、写一个方法生成代理对象(new一个对象出来)。只有生成了真正的对象,才可以进行使用。
!!!new一个对象的步骤:
3.1>、创建LogProxy对象。
3.2>、设置代理对象。
3.3>、创建代理对象。
·invoke是反射里面的方法,通过invoke方法可以去获取实体类里面的方法。
·范例:动态代理的一个类:
package com.wk.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class LogProxy implements InvocationHandler{
private Object target;
private LogProxy(){
}
public static Object getInstance(Object obj){
//3.1创建LogProxy对象
LogProxy proxy = new LogProxy();
//3.2设置代理对象
proxy.target = obj;
//3.3创建代理对象
Object result = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), proxy);
return result;
}
/**
* 当有了代理对象之后,不管代理对象执行什么方法,都会默认调用下面的invoke方法。
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
Info.info("张三+2017年10月19日");
Object obj = method.invoke(target,args);
return obj;
}
}
!!配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 打开注解 -->
<context:annotation-config/>
<!-- 到哪包下找注解 -->
<context:component-scan base-package="com.wk"></context:component-scan>
<!-- 加载动态代理类 -->
<!-- 通过方法创建对象:factory-method -->
<bean id = "userProxy" class = "com.wk.aop.LogProxy" factory-method="getInstance">
<constructor-arg ref = "UserDao"></constructor-arg>
</bean>
<bean id = "roleProxy" class = "com.wk.aop.LogProxy" factory-method="getInstance">
<constructor-arg ref = "RoleDao"></constructor-arg>
</bean>
</beans>
!!测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.action.Action;
import com.wk.action.RoleAction;
import com.wk.model.Role;
import com.wk.model.user;
public class TestUser {
@Test
public void test02() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
Action userAction = factory.getBean("Action", Action.class);
user user = new user(1,"张三","123");
userAction.setUser(user);
userAction.add();
Action userAction2 = factory.getBean("Action", Action.class);
//userAction2.add();
System.out.println(userAction == userAction2);
}
@Test
public void test03() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
RoleAction roleAction = factory.getBean("RoleAction", RoleAction.class);
Role role = new Role(1,"张三");
roleAction.setRole(role);
roleAction.add();
roleAction.delete();
}
}
16:00 2017/10/19 动态AOP代理
/*****************************************************************************************************/
·多个类的话静态切面编程较为麻烦,所以使用动态切面编程。
·动态代理步骤:
(1)、概念:概念上来讲,通过代理对象,创建需要的业务对象,然后通过代理对象,进行各种需求的处理。
(2)、创建的过程(创建代理对象):
<1>、写一个类实现invocationHandler接口。
<2>、在这个类里面创建代理对象(只是声明)。
<3>、写一个方法生成代理对象(new一个对象出来)。只有生成了真正的对象,才可以进行使用。
!!!new一个对象的步骤:
3.1>、创建LogProxy对象。
3.2>、设置代理对象。
3.3>、创建代理对象。
·invoke是反射里面的方法,通过invoke方法可以去获取实体类里面的方法。
·范例:动态代理的一个类:
package com.wk.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class LogProxy implements InvocationHandler{
private Object target;
private LogProxy(){
}
public static Object getInstance(Object obj){
//3.1创建LogProxy对象
LogProxy proxy = new LogProxy();
//3.2设置代理对象
proxy.target = obj;
//3.3创建代理对象
Object result = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), proxy);
return result;
}
/**
* 当有了代理对象之后,不管代理对象执行什么方法,都会默认调用下面的invoke方法。
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
Info.info("张三+2017年10月19日");
Object obj = method.invoke(target,args);
return obj;
}
}
!!配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 打开注解 -->
<context:annotation-config/>
<!-- 到哪包下找注解 -->
<context:component-scan base-package="com.wk"></context:component-scan>
<!-- 加载动态代理类 -->
<!-- 通过方法创建对象:factory-method -->
<bean id = "userProxy" class = "com.wk.aop.LogProxy" factory-method="getInstance">
<constructor-arg ref = "UserDao"></constructor-arg>
</bean>
<bean id = "roleProxy" class = "com.wk.aop.LogProxy" factory-method="getInstance">
<constructor-arg ref = "RoleDao"></constructor-arg>
</bean>
</beans>
!!测试类:
package com.wk.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wk.action.Action;
import com.wk.action.RoleAction;
import com.wk.model.Role;
import com.wk.model.user;
public class TestUser {
@Test
public void test02() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
Action userAction = factory.getBean("Action", Action.class);
user user = new user(1,"张三","123");
userAction.setUser(user);
userAction.add();
Action userAction2 = factory.getBean("Action", Action.class);
//userAction2.add();
System.out.println(userAction == userAction2);
}
@Test
public void test03() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
RoleAction roleAction = factory.getBean("RoleAction", RoleAction.class);
Role role = new Role(1,"张三");
roleAction.setRole(role);
roleAction.add();
roleAction.delete();
}
}
阅读全文
0 0
- Spring动态代理对象
- spring框架Proxy-代理模式(动态生成代理对象)
- 【spring】静态代理&&动态代理
- spring静态代理 动态代理
- spring静态代理 动态代理
- spring静态代理 动态代理
- spring静态代理 动态代理
- Spring 关于动态代理
- Spring中的动态代理
- Spring动态代理
- Spring 动态代理
- Spring 中的动态代理
- spring动态代理
- spring动态代理
- spring动态代理基本原理
- Spring动态代理实现
- Spring动态代理使用
- Spring Aop 动态代理
- sl4j日志可在本地写入控制台和文件,在服务器不能写入文件
- 在eclipse里面启动服务器的时候报启动45秒错误
- redis2
- 任学堂说科技:人工智能的火“烧”到了英国,你却还在无动于衷
- vue 常用Api整理 3
- Spring动态代理对象
- 数据结构与C语言实现(六)——树(下):题目:堆中的路径
- #转自JIONSN的博客#在pycharm中配置Anaconda以及pip源配置
- 判断闰年
- Spring Boot 静态资源处理
- mybatis choose用法
- 2017.10.19 測試總結并今日總結
- 用float和margin实现左右布局
- 高度固定的div垂直居中