Spring的传统AOP的自动创建代理

当需要代理的对象特别多时,每次都创建一个ProxyFactoryBean.工作量会比较大。所以,需要动态代理。

1723f750cf15e5887d385762af7e3dd8a3b.jpg

===========BeanNameAutoProxyCreator===========

package com.imooc.aop.demo5;

public class CustomerDao {
    public void find(){
        System.out.println("查询客户...");
    }

    public void save(){
        System.out.println("保存客户...");
    }

    public void update(){
        System.out.println("修改客户...");
    }

    public void delete(){
        System.out.println("删除客户...");
    }
}
package com.imooc.aop.demo5;

public interface StudentDao {
    public void find();
    public void save();
    public void update();
    public void delete();
}
package com.imooc.aop.demo5;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void find() {
        System.out.println("学生查询...");
    }

    @Override
    public void save() {
        System.out.println("学生保存。。。");
    }

    @Override
    public void update() {
        System.out.println("学生修改。。。");
    }

    @Override
    public void delete() {
        System.out.println("学生删除。。。。");
    }
}
package com.imooc.aop.demo5;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class MyBeforeAdvice implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强=========");
    }
}
package com.imooc.aop.demo5;


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

public class MyAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("环绕前增强=================");
        Object obj=invocation.proceed();
        System.out.println("环绕后增强=================");
        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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置目标类-->
    <bean id="studentDao" class="com.imooc.aop.demo5.StudentDaoImpl"/>
    <bean id="customerDao" class="com.imooc.aop.demo5.CustomerDao"/>

    <!--配置增强-->
    <bean id="myBeforeAdvice" class="com.imooc.aop.demo5.MyBeforeAdvice"/>
    <bean id="myAroundAdvice" class="com.imooc.aop.demo5.MyAroundAdvice"/>

    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames" value="*Dao"/>
        <!--<property name="interceptorNames"  value="myBeforeAdvice,myAroundAdvice"/>-->
        <property name="interceptorNames"  value="myBeforeAdvice"/>
    </bean>

</beans>
package com.imooc.aop.demo5;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class springDemo5 {
    @Resource(name="studentDao")
    private StudentDao studentDao;
    @Resource(name="customerDao")
    private CustomerDao customerDao;

    @Test
    public void demo1(){
        studentDao.find();
        studentDao.save();
        studentDao.update();
        studentDao.delete();

        customerDao.find();
        customerDao.save();
        customerDao.update();
        customerDao.delete();
    }
}

================DefaultAdvisorAutoProxyCreator================

package com.imooc.aop.demo6;

public class CustomerDao {
    public void find(){
        System.out.println("查询客户...");
    }

    public void save(){
        System.out.println("保存客户...");
    }

    public void update(){
        System.out.println("修改客户...");
    }

    public void delete(){
        System.out.println("删除客户...");
    }
}
package com.imooc.aop.demo6;

public interface StudentDao {
    public void find();
    public void save();
    public void update();
    public void delete();
}
package com.imooc.aop.demo6;

public class StudentDaoImpl implements StudentDao {
    @Override
    public void find() {
        System.out.println("学生查询...");
    }

    @Override
    public void save() {
        System.out.println("学生保存。。。");
    }

    @Override
    public void update() {
        System.out.println("学生修改。。。");
    }

    @Override
    public void delete() {
        System.out.println("学生删除。。。。");
    }
}
package com.imooc.aop.demo6;


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

public class MyAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("环绕前增强=================");
        Object obj=invocation.proceed();
        System.out.println("环绕后增强=================");
        return obj;
    }
}
package com.imooc.aop.demo6;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class MyBeforeAdvice implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强=========");
    }
}
package com.imooc.aop.demo6;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext4.xml")
public class SpringDemo6 {
    @Resource(name="studentDao")
    private StudentDao studentDao;
    @Resource(name="customerDao")
    private CustomerDao customerDao;

    @Test
    public void demo1(){
        studentDao.find();
        studentDao.save();
        studentDao.update();
        studentDao.delete();

        customerDao.find();
        customerDao.save();
        customerDao.update();
        customerDao.delete();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置目标类-->
    <bean id="studentDao" class="com.imooc.aop.demo6.StudentDaoImpl"/>
    <bean id="customerDao" class="com.imooc.aop.demo6.CustomerDao"/>

    <!--配置增强-->
    <bean id="myBeforeAdvice" class="com.imooc.aop.demo6.MyBeforeAdvice"/>
    <bean id="myAroundAdvice" class="com.imooc.aop.demo6.MyAroundAdvice"/>

    <!--配置切面-->
    <bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="pattern" value="com\.imooc\.aop\.demo6\.CustomerDao\.save"/>
        <property name="advice" ref="myAroundAdvice"/>
    </bean>

    <!--基于切面信息动态代理-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">

    </bean>

</beans>

 

 

转载于:https://my.oschina.net/popfei/blog/1830766

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值