Spring(十一)使用Spring的注解方式实现面向切面技术AOP

来源:互联网 发布:淘宝如何设置图片保护 编辑:程序博客网 时间:2024/05/21 23:00

AOP涉及到的一些概念:

Aspect(切面):横切性关注点的抽象即为切面,与类相似,只是两者的关注度不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象。
Joinpoint(连接点):所谓连接点指那些被拦截的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点(实际上Joinpoint还可以是field或类构造器)。
Pointcut(切入点):所谓切入点是指我们要对那些Joinpoint进行拦截的定义。
Advice(通知):所谓通知是指拦截到Joinpoint之后所要做的事情。通知分为前置通知、后置通知、异常通知、最终通知、环绕通知。
Target(目标对象):代理的目标对象。
Weave(织入):指将Aspect应用到Target对象并导致Proxy对象创建的过程。
Introduction(引入):在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Field。

Spring提供了基于xml配置和基于注解两种方式进行AOP开发。

使用AOP在beans.xml中也有一些自己的配置:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
     <aop:aspectj-autoproxy />
     <bean id="myInterceptor" class="test.spring.aop.MyInterceptor"></bean>
     <bean id="personService" class="test.spring.service.impl.PersonServiceBean"></bean>
</beans> 

导入一些必须的jar包->

package test.spring.service;public interface PersonService {public void save(String name);public String getResult();}

package test.spring.service.impl;import test.spring.service.PersonService;//代理对象实现目标对象所有接口public class PersonServiceBean implements PersonService {public PersonServiceBean() {}@Overridepublic void save(String name) {System.out.println("save()->>" + name);throw new RuntimeException(">>----自定义异常----<<");}@Overridepublic String getResult() {return "getResult()==>>返回结果";}}
切面

package test.spring.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;@Aspectpublic class MyInterceptor {// 切入点@Pointcut("execution (* test.spring.service.impl.PersonServiceBean.*(..))")private void anyMethod() {}// 声明一个切入点// 无参数的切入点// @Before("anyMethod()")// 定义有参数的切入点,参数名称需相同。这里对拦截到的方法只有只有一个String参数的方法才有用@Before("anyMethod() && args(userName)")public void doAccessCheck(String userName) {System.out.println("前置通知-->>" + userName);}// 获取有返回结果的通知@AfterReturning(pointcut = "anyMethod()", returning = "result")public void doAfterReturning(String result) {System.out.println("后置通知-->>" + result);}@After("anyMethod()")public void doAfter() {System.out.println("最终通知");}// 调用的方法出现异常才会调用这个方法@AfterThrowing(pointcut = "anyMethod()", throwing = "e")public void doAfterThrowing(Exception e) {System.out.println("异常通知-->" + e);}@Around("anyMethod()")public Object doAround(ProceedingJoinPoint pJoinPoint) throws Throwable {System.out.println("环绕通知");// 这里如果pJoinPoint.proceed()不执行,后面拦截到的方法都不会执行,非常适用于权限管理Object result = pJoinPoint.proceed();System.out.println("退出");return result;}}

定义完切面,要交给Spring管理才会有效果。交给Spring有两种方式,一种是在beans.xml中定义bean,另一种是通过@Component扫描

package test.spring.junit;import org.junit.Test;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import test.spring.service.PersonService;public class AOPTest3 {@Testpublic void test() {AbstractApplicationContext aContext = //new ClassPathXmlApplicationContext("beans.xml");PersonService pService = (PersonService) aContext.getBean("personService");pService.save("LinDL");// pService.getResult();aContext.close();}}
测试save方法



测试getResult方法


0 0
原创粉丝点击