动态代理

静态代理
spring06staticproxy包
Subject接口
package cn.happy.spring06staticproxy;
/**
 * 抽象主题
 * Created by Happy on 2017-07-30.
 */
public interface Subject {
    public String add();
}

RealSubject
package cn.happy.spring06staticproxy;

/**
 * Created by Happy on 2017-07-30.
 */
public class RealSubject implements Subject {
    public String add() {
        System.out.println("add ok!");
        return "";
    }
}
ProxySubject
package cn.happy.spring06staticproxy;

/**
 * Created by Happy on 2017-07-30.
 */
public class ProxySubject implements Subject {
    //对象间交互
    private Subject realSubject;
    public String add() {
        System.out.println("事务已经开启");
        return realSubject.add();
    }

    public Subject getRealSubject() {
        return realSubject;
    }

    public void setRealSubject(Subject realSubject) {
        this.realSubject = realSubject;
    }
}
测试
 @Test
    public void test01() {
        //真实主题对象
        Subject subject=new RealSubject();
        //代理对象
        ProxySubject proxySubject=new ProxySubject();

        proxySubject.setRealSubject(subject);
        proxySubject.add();
    }
JDK动态代理
spring07jdkdynamicproxy包
IUserDAO接口
package cn.happy.spring07jdkdynamicproxy;

/**
 * Created by Happy on 2017-07-30.
 */
public interface IUserDAO {
    public String add();
    public String edit();
}
UserDAOImpl
package cn.happy.spring07jdkdynamicproxy;

/**
 * Created by Happy on 2017-07-30.
 */
public class UserDAOImpl implements IUserDAO {
        public String add() {
            System.out.println("add");
            return "add";
        }

        public String edit() {
            System.out.println("edit");
            return "edit";
        }
}
测试
@Test
    public void test02() {
        final IUserDAO dao=new UserDAOImpl();
        IUserDAO proxy=(IUserDAO)Proxy.newProxyInstance(dao.getClass().getClassLoader(), dao.getClass().getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("事务已经开启");
                method.invoke(dao,args);
                return null;
            }
        });
        //代理对象add   edit
        proxy.add();
        proxy.edit();
    }
CGLIB动态代理
spring08cglibdynamicproxy包
UserService
package cn.happy.spring08cglibdynamicproxy;

/**
 * Created by Happy on 2017-07-30.
 */
public class UserService {
    //核心业务方法
    public void delete(){
        System.out.println("delete ok!");
    }
}
测试
 @Test
    public void test03() {
        final UserService service=new UserService();
        //Enhancer对象
        Enhancer enhancer=new Enhancer();
        //在内存中构建业务类的子类
        enhancer.setSuperclass(service.getClass());
        enhancer.setCallback(new MethodInterceptor() {
            /**
             * @param o  代理对象
             * @param method  代理对象方法
             * @param objects  参数
             * @param methodProxy
             */
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("事务已经开启");
                methodProxy.invoke(service,objects);
                return null;
            }
        });
        UserService proxy=(UserService)enhancer.create();
        proxy.delete();

    }
前置增强
spring09aop01包
ISomeService接口
package cn.happy.spring09aop01;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
    public void doSecond();
}
MyBeforeAdvise
package cn.happy.spring09aop01;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
//前置通知
public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=============log==================");
    }
}
SomeService
package cn.happy.spring09aop01;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void doSecond() {
        System.out.println("doSecond");
    }
}
applicationContextspring07aop01.xml
 <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring09aop01.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.happy.spring09aop01.MyBeforeAdvise"></bean>

    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>
        <!--做怎么样的增强-->
        <property name="interceptorNames" value="beforeAdvice"></property>
        <property name="proxyTargetClass" value="true"></property>
    </bean>
测试
@Test
    // 前置增强
    public void test04(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring07aop01.xml");
        SomeService service = (SomeService) ctx.getBean("proxyService");
        service.doSome();
        service.doSecond();
    }
后置增强
spring10afterreturingadvice
ISomeService接口
package cn.happy.spring10afterreturingadvice;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
}
MyAfterReturningAdvice
package cn.happy.spring10afterreturingadvice;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
public class MyAfterReturningAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("===========after================");
    }
}
SomeService
package cn.happy.spring10afterreturingadvice;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }
}
applicationContextspring08aop02.xml
 <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring10afterreturingadvice.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="afterAdvice" class="cn.happy.spring10afterreturingadvice.MyAfterReturningAdvice"></bean>

    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>
        <!--做怎么样的增强-->
        <property name="interceptorNames" value="afterAdvice"></property>

    </bean>
测试
@Test
    // 后置增强
    public void test05(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring08aop02.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
    }
环绕增强
spring11methodinterceptor包
ISomeService接口
package cn.happy.spring11methodinterceptor;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
}
MyMethodInterceptor
package cn.happy.spring11methodinterceptor;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Created by Happy on 2017-07-30.
 */
public class MyMethodInterceptor implements MethodInterceptor {
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("before");
        methodInvocation.proceed();
        System.out.println("after");
        return null;
    }
}
SomeService
package cn.happy.spring11methodinterceptor;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }
}
applicationContextspring09aop03.xml
<!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring11methodinterceptor.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="methodAdvice" class="cn.happy.spring11methodinterceptor.MyMethodInterceptor"></bean>

    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>
        <!--做怎么样的增强-->
        <property name="interceptorNames" value="methodAdvice"></property>

    </bean>
测试
@Test
    // 环绕增强
    public void test06(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring09aop03.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
    }
异常增强
spring12throwadvice包
IHHJJ接口
package cn.happy.spring12throwadvice;

/**
 * Created by Happy on 2017-07-30.
 */
public interface IHHJJ {
    public void run();
    public void run(String style);
}
ISomeService接口
package cn.happy.spring12throwadvice;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
}
HHJJImpl
package cn.happy.spring12throwadvice;

/**
 * Created by Happy on 2017-07-30.
 */
public class HHJJImpl implements IHHJJ {
    public void run() {

    }

    public void run(String style) {

    }
}

MyThrowsAdvice
package cn.happy.spring12throwadvice;

import org.springframework.aop.ThrowsAdvice;

/**
 * Created by Happy on 2017-07-30.
 */
public class MyThrowsAdvice implements ThrowsAdvice {
    public void afterThrowing(Exception ex){
        System.out.println("错误");
    }

}
SomeService
package cn.happy.spring12throwadvice;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        int result=5/0;
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }
}

applicationContextspring10aop04.xml
<!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring12throwadvice.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="throwsAdvice" class="cn.happy.spring12throwadvice.MyThrowsAdvice"></bean>

    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>
        <!--做怎么样的增强-->
        <property name="interceptorNames" value="throwsAdvice"></property>

    </bean>

测试
@Test
    // 异常增强
    public void test07(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring10aop04.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
    }
名称匹配方法切入点顾问
spring13aopadvisor
ISomeService接口
package cn.happy.spring13aopadvisor;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
    public void add();
}
MyBeforeAdvise
package cn.happy.spring13aopadvisor;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
//前置通知
public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=============log==================");
    }
}
SomeService
package cn.happy.spring13aopadvisor;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void add() {
        System.out.println("==============add============");
    }
}

applicationContextspring11advisor01.xml
<!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring13aopadvisor.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.happy.spring13aopadvisor.MyBeforeAdvise"></bean>

    <!--02.增强 :顾问Advisor:包装通知-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice" ></property>
        <property name="mappedNames" value="doSome2"></property>
    </bean>

    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>
        <!--做怎么样的增强-->
        <property name="interceptorNames" value="beforeAdvisor"></property>

    </bean>

测试
 @Test
    //名称匹配方法切入点顾问
    public void test01(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring11advisor01.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
        service.add();
    }
正则表达式匹配方法切入点顾问
spring14aopadvisor02regex
ISomeService接口
package cn.happy.spring14aopadvisor02regex;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
    public void add();
}
MyBeforeAdvise
package cn.happy.spring14aopadvisor02regex;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
//前置通知
public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=============log==================");
    }
}

SomeService
package cn.happy.spring14aopadvisor02regex;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void add() {
        System.out.println("==============add============");
    }
}

applicationContextspring12advisor02.xml
 <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring14aopadvisor02regex.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.happy.spring14aopadvisor02regex.MyBeforeAdvise"></bean>

    <!--增强 顾问-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="pattern" value=".*d.*" ></property>
    </bean>
    <!--03.aop -->
    <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置需要增强的目标对象-->
        <property name="target" ref="someService"></property>

        <!--做怎么样的增强-->
        <property name="interceptorNames" value="beforeAdvisor"></property>

    </bean>

测试
 @Test
    //正则表达式匹配方法切入点顾问
    public void test02(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring12advisor02.xml");
        ISomeService service = (ISomeService) ctx.getBean("proxyService");
        service.doSome();
        service.add();
    }
默认Advisor自动代理生成器
spring15aopauto01包
ISomeService接口
package cn.happy.spring15aopauto01;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
    public void add();
}
MyAfterReturningAdvice
package cn.happy.spring15aopauto01;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
public class MyAfterReturningAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("===========after================");
    }
}
MyBeforeAdvise
package cn.happy.spring15aopauto01;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
//前置通知
public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=============log==================");
    }
}
SomeService
package cn.happy.spring15aopauto01;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void add() {
        System.out.println("==============add============");
    }
}
applicationContextspring13auto01.xml
<!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring15aopauto01.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.happy.spring15aopauto01.MyBeforeAdvise"></bean>
    <bean id="afterAdvice" class="cn.happy.spring15aopauto01.MyAfterReturningAdvice"></bean>
  <!--增强:前置顾问-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="pattern" value=".*do.*"></property>
    </bean>

    <!--增强:后置顾问-->
    <bean id="afterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="afterAdvice"></property>
        <property name="pattern" value=".*do.*"></property>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

测试
@Test
    // 03.默认Advisor自动代理生成器
    public void test03(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring13auto01.xml");
        ISomeService service = (ISomeService) ctx.getBean("someService");
        service.doSome();
        service.add();
    }
BeanName自动代理生成器
spring16aopbeannameaop包
ISomeService接口
package cn.happy.spring16aopbeannameaop;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
    public void add();
}
MyBeforeAdvise

package cn.happy.spring16aopbeannameaop;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * Created by Happy on 2017-07-30.
 */
//前置通知
public class MyBeforeAdvise implements MethodBeforeAdvice {

    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("=============log==================");
    }
}
SomeService
package cn.happy.spring16aopbeannameaop;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
    }

    public void add() {
        System.out.println("==============add============");
    }
}
applicationContextspring14auto02.xml
 <!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring16aopbeannameaop.SomeService"></bean>

    <!--02.增强 通知-->
    <bean id="beforeAdvice" class="cn.happy.spring16aopbeannameaop.MyBeforeAdvise"></bean>

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <!--目标对象-->
        <property name="beanNames" value="someService"></property>
        <property name="interceptorNames" value="beforeAdvice"></property>
    </bean>

测试
@Test
    // BeanName自动代理生成器
    public void test04(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring14auto02.xml");
        ISomeService service = (ISomeService) ctx.getBean("someService");
        service.doSome();
        service.add();
    }
aspectj 注解
spring17aspectj包
ISomeService接口
package cn.happy.spring17aspectj;

/**
 * Created by Happy on 2017-07-30.
 */
public interface ISomeService {
    public void doSome();
    public String add();
}

MyAspect
package cn.happy.spring17aspectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * Created by Happy on 2017-07-31.
 */
@Aspect
public class MyAspect {

    //前置增强
    //@Before(value = "execution(* *..spring17aspectj.*.*(..))")
    public void myBefore(){
        System.out.println("===我是前置增强内容======");
    }
    //后置增强
    //@AfterReturning(value = "execution(* *..spring17aspectj.*.*(..))")
    public void myAferReturing(){
        System.out.println("===我是after内容======");
    }

    //环绕增强
    @Around(value = "execution(* *..spring17aspectj.*.*(..))")
    public Object myAround(ProceedingJoinPoint proceed) throws Throwable {
        System.out.println("===我是环绕前内容======");
        Object result = proceed.proceed();
        System.out.println("===我是环绕后内容======");
        if (result!=null){
            String str=(String)result;
            return str.toUpperCase();
        }else{
            return null;
        }
    }
}

SomeService
package cn.happy.spring17aspectj;

/**
 * Created by Happy on 2017-07-30.
 */
public class SomeService implements ISomeService {
    //核心业务
    public void doSome(){
        System.out.println("北京海淀区五道口");
    }

    public String add() {
        System.out.println("==============add============");
        return "add";
    }
}
applicationContextspring15aspect.xml
<!--01.目标对象-->
    <bean id="someService" class="cn.happy.spring17aspectj.SomeService"></bean>

    <!--02.增强 通知-->
    <bean class="cn.happy.spring17aspectj.MyAspect"></bean>

    <aop:aspectj-autoproxy/>
测试
@Test
    //aspectj 注解
    public void test05(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring15aspect.xml");
        ISomeService service = (ISomeService) ctx.getBean("someService");
        service.doSome();
        String result = service.add();
        System.out.println(result);
    }


















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值