Spring Aop

来源:互联网 发布:群优化 编辑:程序博客网 时间:2024/05/30 23:12

spring框架具有面向切面编程,即aop的功能,有两种方式可以实现,一种是基于XML配置aop,另外一种是使用注解。

除了spring框架的核心jar包之外,还需要导入以下jar包: aopalliance.jar    aspectjrt.jar    aspectjweaver.jar    spring-aop-3.2.5.RELEASE.jar

基于XML方式,需要导入aop命名空间和使用aop:configs指定切面执行类:

aop执行类:

package yzr.aop;public class Aop {public Aop(){}public void before(){System.out.println("执行之前....");}public void after(){System.out.println("执行之后....");}}

  配置文件,注意要包含aop命名空间:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="userAction" class="yzr.action.UserAction"></bean><bean id="aop" class="yzr.aop.Aop"></bean><aop:config><aop:pointcut expression="execution(* yzr.action.*.*(..))" id="pointcut"/><aop:aspect ref="aop"><aop:before method="before" pointcut-ref="pointcut"/><aop:after method="after" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans>     
当获取action对象之后执行方法时,aop切面类就会根据aop:config配置的进行工作

package yzr.action;public class UserAction {public String execute() throws Exception{System.out.println("UserAction执行execute");return "success";}}

package yzr.unit;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.Resource;import yzr.action.UserAction;public class app {String path="ApplicationContext.xml";@Testpublic void test() throws Exception{ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext(path);System.out.println("加载配置文件完成");UserAction action=(UserAction) ac.getBean("userAction");action.execute();}}


    

基于注解方式的aop:

@Aspect   指定一个类为切面类
@Pointcut 指定一个切入点表达式语句
@Before   在目标方法之前执行的方法
@After    在目标方法之后执行的方法 (无论有没有出现异常,都会执行)
@Around   环绕目标方法的执行
@AfterReturning  出现异常不执行
@AfterThrowing  出现异常的时候,执行关注点代码

使用方法:在配置文件开启注解扫描,在相应的类贴上注解即可:

package yzr.aop;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Aspect@Componentpublic class Aop {@Pointcut("execution(* yzr.action.*.*(..)) ")public void pointcut() {}@Before("pointcut()")public void before() {System.out.println("执行之前....");}@After("pointcut()")public void after() {System.out.println("执行之后....");}// 环绕目标方法@Around("pointcut()")public void around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("环绕前");pjp.proceed();// 执行目标方法System.out.println("环绕后");}// 在目标方法执行结束后执行@AfterReturning("pointcut()")public void afterReturing() {System.out.println("方法执行结束,最后返回");}// 在目标方法出现异常时候执行@AfterThrowing("pointcut()")public void afterThrowing() {System.out.println("出现异常");}}

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd      http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd         http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="yzr"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- <bean id="userAction" class="yzr.action.UserAction"></bean><bean id="aop" class="yzr.aop.Aop"></bean><aop:config><aop:pointcut expression="execution(* yzr.action.*.*(..))" id="pointcut"/><aop:aspect ref="aop"><aop:before method="before" pointcut-ref="pointcut"/><aop:after method="after" pointcut-ref="pointcut"/></aop:aspect></aop:config>--></beans>     


如果执行结果没有使用到aop的切面方法,请检查pointcut()指定的拦截表达式。

1 0
原创粉丝点击