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() {
-
- }
-
- @Override
- public void save(String name) {
- System.out.println("save()->>" + name);
- throw new RuntimeException(">>----自定义异常----<<");
- }
-
- @Override
- public 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;
- @Component
- @Aspect
- public class MyInterceptor {
-
- @Pointcut("execution (* test.spring.service.impl.PersonServiceBean.*(..))")
- private void anyMethod() {
- }
-
-
-
-
- @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("环绕通知");
-
- 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 {
-
- @Test
- public void test() {
- AbstractApplicationContext aContext =
- new ClassPathXmlApplicationContext("beans.xml");
- PersonService pService = (PersonService) aContext
- .getBean("personService");
- pService.save("LinDL");
-
- aContext.close();
- }
-
- }
测试save方法
测试getResult方法
@Component 用于将此类注入到spring中,交由spring进行实例化,方法调用.
@Pointcut("execution (* test.spring.service.impl.PersonServiceBean.*(..))") 当执行("")中的方法时,获取到切点,此时此类的某些方法会被调用.
@Before 前置执行
@After 后置执行
@Around 环绕(同时执行)