四:代理

 概念:一种设计模式

1.静态代理:代理模式

代理模式:为其他对象提供一种代理以控制对这个对象的访问,代理模式在访问对象时引入一一定的程度的间接性,因为这种间接性可以附加多种用途

 

2.

实现代码:

第一布:

 动态代理,静态代理

第二步:

 动态代理,静态代理

第三步:

 动态代理,静态代理

总结:

 动态代理,静态代理

2.动态代理(Aop)

  (1)JDk

 动态代理,静态代理

实现代码:

第一步:

 动态代理,静态代理

第二步:

 动态代理,静态代理

第三步:

 动态代理,静态代理

  (2)CgLib

 

 动态代理,静态代理

 动态代理,静态代理

 

 

单测:

@Test
public void cglib(){
     final cglib cglib=newcglib();
    //创建代理
    //工具类
    
Enhancer enhancer=newEnhancer();
    enhancer.setSuperclass(cglib.getClass());
    enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler(){
        public Object invoke(Object o, Method method, Object[] objects)throwsThrowable{
            System.out.println("-------------------before------------");
            Object resullt=method.invoke(cglib);
            System.out.println("--------------after------------------");
            return resullt;
        }
});
    //提供一个方法
    
cglib proxy=(cglib)enhancer.create();
    proxy.show();
}

3.aop的练习

类:

 

 动态代理,静态代理


 动态代理,静态代理

动态代理,静态代理


 动态代理,静态代理

Xml

<!--*********Aop金典练习*******-->
    
<beanid="lei"class="cn.aop.aop1.lei"></bean>
    <beanid="before"class="cn.aop.aop1.Before"></bean>
    <beanid="After"class="cn.aop.aop1.After"></bean>
    <beanid="zengqiang"class="org.springframework.aop.framework.ProxyFactoryBean">
        <propertyname="target"ref="lei"></property>
        <propertyname="interceptorNames"value="before,After"></property>
        <!--单测时不用接口-->
        
<propertyname="proxyTargetClass"value="true"/>
    </bean>
    <!--环绕增强-->
    
<beanid="jiekou"class="cn.aop.huanrao.lei"></bean>
    <beanid="huanrao1"class="cn.aop.huanrao.leiimp"></bean>
    <beanid="huanrao"class="org.springframework.aop.framework.ProxyFactoryBean">
        <propertyname="target"ref="jiekou"></property>
        <propertyname="interceptorNames"value="huanrao1"/>
    </bean>

 

单测:

 

 动态代理,静态代理

 

4.顾问:

类:

 动态代理,静态代理

 动态代理,静态代理

 动态代理,静态代理

Xml

 动态代理,静态代理

 

 

Test

 动态代理,静态代理

5.自定义:

:

 

 动态代理,静态代理


 动态代理,静态代理

动态代理,静态代理

 

Xml:

 动态代理,静态代理

6.Aspectj

 

 动态代理,静态代理


 

 动态代理,静态代理


 

Aspectj类:

 

 动态代理,静态代理

 

 

Xml

 动态代理,静态代理

 

<aop:aspectj-autoproxy/>

Test:

 

 动态代理,静态代理

 

 

 

 

相关文章:

  • 2021-09-14
  • 2021-12-26
  • 2021-06-13
猜你喜欢
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2021-06-05
  • 2021-12-29
相关资源
相似解决方案