Spring AOP 概念与例子

先了解AOP的相关术语:

1.通知(Advice):
通知定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。
2.连接点(Joinpoint):
程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。
3.切入点(Pointcut)
通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,Spring中允许我们方便的用正则表达式来指定
4.切面(Aspect)
通知和切入点共同组成了切面:时间、地点和要发生的“故事”
5.引入(Introduction)
引入允许我们向现有的类添加新的方法和属性(Spring提供了一个方法注入的功能)
6.目标(Target)
即被通知的对象,如果没有AOP,那么它的逻辑将要交叉别的事务逻辑,有了AOP之后它可以只关注自己要做的事(AOP让他做爱做的事)
7.代理(proxy)
应用通知的对象,详细内容参见设计模式里面的代理模式
8.织入(Weaving)
把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:
(1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器
(2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码
(3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术

Spring提供了4种实现AOP的方式:
1.经典的基于代理的AOP
2.@AspectJ注解驱动的切面
3.纯POJO切面
4.注入式AspectJ切面

首先看经典的基于代理的AOP:
Spring支持五种类型的通知:
Before(前)  org.apringframework.aop.MethodBeforeAdvice
After-returning(返回后) org.springframework.aop.AfterReturningAdvice
After-throwing(抛出后) org.springframework.aop.ThrowsAdvice
Arround(周围) org.aopaliance.intercept.MethodInterceptor
Introduction(引入) org.springframework.aop.IntroductionInterceptor

aop要怎么玩?就几个步骤而已!

1.创建通知:实现这几个接口,把其中的方法实现了
2.定义切点和通知者:在Spring配制文件中配置这些信息
3.使用ProxyFactoryBean来生成代理

@Aspect
@Component
public class WebLogAspect {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Pointcut("execution(public * com.oys.controller..*.*(..))")
	public void printLog() {
	}

	@Before("printLog()")
	public void doBefore(JoinPoint joinPoint) throws Throwable {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		logger.info("URL : " + request.getRequestURL().toString()+",HTTP_METHOD : " + request.getMethod()+",IP : " + request.getRemoteAddr()+",QueryString : " + request.getQueryString());
		Enumeration<String> enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			logger.info("name:{},value:{}", name, request.getParameter(name));
		}
	}

	@AfterReturning(returning = "ret", pointcut = "printLog()")
	public void doAfterReturning(Object ret) throws Throwable { 
		logger.info("RESPONSE : " + ret);
	}
 
	@Around("printLog()")
	public void around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("begin"); 
		joinPoint.proceed(); 
		System.out.println("commit");
	}
}

//<aop:aspectj-autoproxy />
//<aop:config>
//	<aop:pointcut expression="execution(* com.oys.service.*.*.*(..))" id="pt" />
//	<aop:aspect ref = "transactionDemo">
//	<aop:before method="startTransaction" pointcut-ref="pt" />
//	<aop:after-returning method="commitTransaction" pointcut-ref="pt"/>
//	</aop:aspect>
//</aop:config>


//<aop:config>
//    <aop:aspect ref="log">
//        <aop:pointcut expression="(execution(* com.oys.service.*.*.*(..)))" id="pt" />
//        <aop:before method="washOven" pointcut-ref="pt" />
//        <aop:before method="prepare" pointcut-ref="pt" />
//        <aop:after method="after" pointcut-ref="pt" />
//    </aop:aspect>
//</aop:config>

 

public   class  BeforeAdvice  implements  MethodBeforeAdvice {
    public   void  before(Method method,Object[] args, Object target) throws  Throwable {
        System.out.println( " 这是BeforeAdvice类的before方法. " );
    } 
}

public class AfterAdvice implements AfterReturningAdvice{
    public void afterReturning(Object returnValue ,Method method,Object[] args,Object target) throws Throwable{
        System.out.println("这是AfterAdvice类的afterReturning方法.");
    }
}

public class AroundInterceptor implements MethodInterceptor{
    public Object invoke(MethodInvocation invocation) throws Throwable{
        Object result = null;
        String id= invocation.getArguments()[0].toString();
        if ( id.equals("1")){
            result= invocation.proceed();
        } else{
            System.out.println("不是第一条");
        }
        return result;
    }
}



<bean id="before" class="com.oys.aop.BeforeAdvice"></bean>
<bean id="after" class="com.oys.aop.AfterAdvice"></bean>
<bean id="around" class="com.oys.aop.AroundInterceptor"></bean>
<bean id="targetUser" class="com.oys.service.UserImpl"></bean>

<bean id="upb" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="proxyInterfaces">
		<value>com.oys.service.IUser</value>
	</property>
	<property name="interceptorNames">
		<list>
			<value>before</value>
			<value>after</value>
			<value>around</value>  
		</list>
	</property>
	<property name="target">
		<ref bean="targetUser"/>
	</property>
</bean>

 

猜你喜欢

转载自action-java.iteye.com/blog/2289975