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();

}
}
   
原创粉丝点击