---
title: "spring中AOP实现的4中方式"
author: "weinan"
date: "2015-10-14T09:42:00Z"
tags: ["AOP"]
categories: ["移动开发"]
slug: "spring-aop-4"
draft: false
lastmod: "2015-10-14T09:44:40Z"
id: 2248974
url: "https://weinan.iteye.com/blog/2248974"
---

\[quote]配置可以通过xml文件来进行，大概有四种方式： 1. 配置ProxyFactoryBean，显式地设置advisors, advice, target等 2. 配置AutoProxyCreator，这种方式下，还是如以前一样使用定义的bean，但是从容器中获得的其实已经是代理对象 3. 通过来配置，使用AspectJ的注解来标识通知及切入点 4. 通过来配置\[/quote] \[b]\[size=large]一、基于代理的AOP\[/size]\[/b] 1、创建通知 \[code="java"]package com.demo.aop.bean; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; /\** * 通知 * \*/ public class ActionAdvice implements MethodBeforeAdvice,AfterReturningAdvice{ public void afterReturning(Object returnValue, Method method, Object\[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println("执行方法之后调用"+System.currentTimeMillis()); } public void before(Method method, Object\[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println("执行方法之前调用"+System.currentTimeMillis()); } }\[/code] 2、Service层 \[code="java"] package com.demo.aop.service; public interface IPersonService { public void doUpdate(); } \[/code] 3、Service实现层 \[code="java"]package com.demo.aop.service.impl; import org.springframework.stereotype.Component; import com.demo.aop.service.IPersonService; public class PersonService implements IPersonService { public void doUpdate() { // TODO Auto-generated method stub System.out.println("执行更新方法"+System.currentTimeMillis()); } }\[/code] 4、文件配置 第一种：不实用自动代理 \[code="java"] \[/code] 第二种：使用自动代理 \[code="java"] 　\[/code] 5、测试 第一种：不使用自动代理情况下，注意创建对象时要将代理注入 \[code="java"]package com.demo.aop.AopTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.demo.aop.service.IPersonService; /\** * 测试基于代理的AOP * @author wwl * \*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" }) public class AopTest { /\** * 用接口声明，注入代理对象 \*/ @Autowired private IPersonService actionProxy; @Test public void testAop(){ actionProxy.doUpdate(); } }\[/code] 第二种：使用自动代理 \[code="java"]package com.demo.aop.AopTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.demo.aop.service.IPersonService; /\** * 测试基于代理的AOP * @author wwl * \*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/context-aop.xml" }) public class AopTest { /\** * 自动创建代理 \*/ @Autowired private IPersonService personService; @Test public void testAop(){ personService.doUpdate(); } } \[/code] \[b]\[size=large]二、使用AspectJ提供的注解\[/size]\[/b] 1、创建通知 \[code="java"]package com.demo.aop.bean; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /\** * 通知 * \*/ @Component @Aspect public class Action1Advice{ @Pointcut("execution(* \*.doUpdate())") public void actionPoint(){} @AfterReturning("actionPoint()") public void afterAction(){ System.out.println("执行方法之后调用"+System.currentTimeMillis()); } @Before("actionPoint()") public void beforeAction(){ System.out.println("执行方法之前调用"+System.currentTimeMillis()); } }\[/code] 2、Service层 \[code="java"] package com.demo.aop.service; public interface IPersonService { public void doUpdate(); } \[/code] 3、Service实现层 \[code="java"]package com.demo.aop.service.impl; import org.springframework.stereotype.Component; import com.demo.aop.service.IPersonService; @Component public class PersonService implements IPersonService { public void doUpdate() { // TODO Auto-generated method stub System.out.println("执行更新方法"+System.currentTimeMillis()); } }\[/code] 4、文件配置 \[code="java"] \[/code] 5、测试 \[code="java"]package com.demo.aop.AopTest; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.demo.aop.service.IPersonService; /\** * 测试使用AspectJ提供的注解切面 * @author wwl * \*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/spring/context-aop1.xml" }) public class Aop1Test { @Autowired private IPersonService personService; @Test public void testAop(){ personService.doUpdate(); } }\[/code] \[size=large]\[b] 三、纯POJO切面（不使用注解，只在配置中注入）\[/b]\[/size] 1、创建通知 \[code="java"]package com.demo.aop.bean; import org.springframework.stereotype.Component; /\** * 通知 * \*/ @Component public class Action1Advice{ public void afterAction(){ System.out.println("执行方法之后调用"+System.currentTimeMillis()); } public void beforeAction(){ System.out.println("执行方法之前调用"+System.currentTimeMillis()); } }\[/code] 2、Service层 \[code="java"]...........\[/code] 3、Service实现层 \[code="java"]...........\[/code] 4、文件配置 \[code="java"] 　 \[/code] 5、测试 \[quote] ... 总结：总体看其实两种方式，一种是使用代理，一种是使用AOP（是否使用注解）。 流程：先创建通知，创建切点，再将通知与切点绑定使用代理进行处理调用\[/quote]