Spring AOP

1.JDK动态代理

针对实现了接口的类产生代理,代理类实现InvacationHandler接口。
dao接口

public interface CustomerDao {
    void save();

    void update();
}

dao实现类

@Repository
public class CustomerDaoImpl implements CustomerDao {
    @Override
    public void save() {
        System.out.println("执行save()");
    }

    @Override
    public void update() {
        System.out.println("执行update()");
    }
}

service接口

public interface CustomerService {
    void save();

    void update();
}

service实现类

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public void save() {
        customerDao.save();
    }

    @Override
    public void update() {
        customerDao.update();
    }
}

代理类

@Component
public class JdkProxy implements InvocationHandler {

    //目标对象
    @Autowired
    private CustomerService customerService;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("前置增强");
        Object invoke = method.invoke(customerService, args);
        System.out.println("后置增强");
        return invoke;
    }
}

配置类

@Configuration
@ComponentScan(basePackages = {"com.zhy"})
public class SpringConfig {
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class MyTest {

    //真实角色
    @Autowired
    private CustomerService customerService;

    //代理角色
    @Autowired
    private JdkProxy jdkProxy;

    @Test
    public void test(){
        CustomerService service = (CustomerService) Proxy.newProxyInstance(customerService.getClass().getClassLoader(), customerService.getClass().getInterfaces(), jdkProxy);
        service.save();
        service.update();
    }

}

运行结果


运行结果.PNG

2.CGlib动态代理

针对没有实现接口的类产生代理,代理类实现MethodInterceptor接口。
普通类

@Component
public class Customer {
    public void save(){
        System.out.println("执行save()");
    }

    public void update(){
        System.out.println("执行update()");
    }
}

代理类

@Component
public class CglibProxy implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("前置增强");
        Object o1 = methodProxy.invokeSuper(o, objects);
        System.out.println("后置增强");
        return o1;
    }
}

配置类

@Configuration
@ComponentScan(basePackages = {"com.zhy"})
public class SpringConfig {
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class MyTest {

    //真实角色
    @Autowired
    private Customer customer;

    //代理角色
    @Autowired
    private CglibProxy cglibProxy;

    @Test
    public void test1(){
        Customer c = (Customer) Enhancer.create(customer.getClass(), cglibProxy);
        c.save();
        c.update();
    }

}

运行结果


运行结果.PNG

3.基于XML配置文件的Spring AOP

dao接口

public interface CustomerDao {
    void save();

    void update();
}

dao实现类

public class CustomerDaoImpl implements CustomerDao {
    @Override
    public void save() {
        System.out.println("执行save()");
    }

    @Override
    public void update() {
        System.out.println("执行update()");
    }
}

service接口

public interface CustomerService {
    void save();

    void update();
}

service实现类

public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao;

    public void setCustomerDao(CustomerDao customerDao){
        this.customerDao = customerDao;
    }

    @Override
    public void save() {
        customerDao.save();
    }

    @Override
    public void update() {
        customerDao.update();
    }
}

切面

public class MyAspect {

    public void before(){
        System.out.println("前置通知");
    }

    public void after_returing(){
        System.out.println("后置通知");
    }

    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕增强前");
        point.proceed();
        System.out.println("环绕增强后");
    }

    public void after(){
        System.out.println("最终增强");
    }

    public void after_throwing(){
        System.out.println("异常增强");
    }

}

配置文件

<bean id="customerDao" class="com.zhy.dao.CustomerDaoImpl"></bean>

    <bean id="customerServiceImpl" class="com.zhy.service.CustomerServiceImpl" autowire="byName"></bean>

    <bean id="myAspect" class="com.zhy.aspect.MyAspect"></bean>

    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="mypoint" expression="execution(* com.zhy.service.CustomerServiceImpl.*(..))"/>
        <!--配置切面-->
        <aop:aspect ref="myAspect">
            <!--前置增强-->
            <aop:before method="before" pointcut-ref="mypoint"></aop:before>
            <!--后置增强:出现异常则不增强-->
            <aop:after-returning method="after_returing" pointcut-ref="mypoint"></aop:after-returning>
            <!--环绕增强-->
            <aop:around method="around" pointcut-ref="mypoint"></aop:around>
            <!--最终增强-->
            <aop:after method="after" pointcut-ref="mypoint"></aop:after>
            <!--异常增强-->
            <aop:after-throwing method="after_throwing" pointcut-ref="mypoint"></aop:after-throwing>
        </aop:aspect>
    </aop:config>

测试类

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        CustomerService customerServiceImpl = context.getBean("customerServiceImpl", CustomerService.class);
        customerServiceImpl.save();
        customerServiceImpl.update();
    }

运行结果


运行结果.PNG

4.基于注解的Spring AOP

dao接口

public interface CustomerDao {
    void save();

    void update();
}

dao实现类

@Repository
public class CustomerDaoImpl implements CustomerDao {
    @Override
    public void save() {
        System.out.println("执行save()");
    }

    @Override
    public void update() {
        System.out.println("执行update()");
    }
}

service接口

public interface CustomerService {
    void save();

    void update();
}

service实现类

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public void save() {
        customerDao.save();
    }

    @Override
    public void update() {
        customerDao.update();
    }
}

切面

@Aspect
@Component
public class MyAspect {

    @Pointcut(value = "execution(* com.zhy.service.CustomerServiceImpl.*(..))")
    public void mypoint(){

    }

    @Before(value = "mypoint()")
    public void before(){
        System.out.println("前置通知");
    }

    @AfterReturning(value = "mypoint()")
    public void after_returing(){
        System.out.println("后置通知");
    }

    @Around(value = "mypoint()")
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕增强前");
        point.proceed();
        System.out.println("环绕增强后");
    }

    @After(value = "mypoint()")
    public void after(){
        System.out.println("最终增强");
    }

    @AfterThrowing(value = "mypoint()")
    public void after_throwing(){
        System.out.println("异常增强");
    }

}

配置类

@Configuration
@ComponentScan(basePackages = {"com.zhy"})
@EnableAspectJAutoProxy
public class SpringConfig {
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class MyTest {

    @Autowired
    private CustomerService customerService;

    @Test
    public void test1(){
        customerService.save();
        customerService.update();
    }

}

运行结果


运行结果.PNG
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容