赞
踩
AOP(Aspect Oriented Programming)面向切面编程
通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术
是面向对象(OOP)的延续
AOP 在 Spring 中的作用:
1.提供声明式事务
2.允许用户自定义切面
导入 jar 包,搜索 AspectJ Weaver
(复制过来把 <scope>runtime</scope> 这行删去,不然注解报错)
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.9.22.1</version>
- </dependency>
方法一:使用原生 Spring API 接口实现
先写个 UserService 接口:
- package com.demo.service;
-
- public interface UserService {
- public void add();
- public void delete();
- public void update();
- public void select();
- }
再写个 UserServiceImpl 类来实现接口,重写方法
- package com.demo.service;
-
- public class UserServiceImpl implements UserService{
- @Override
- public void add() {
- System.out.println("增");
- }
-
- @Override
- public void delete() {
- System.out.println("删");
- }
-
- @Override
- public void update() {
- System.out.println("改");
- }
-
- @Override
- public void select() {
- System.out.println("查");
- }
- }

写个 Log 类来实现 MethodBeforeAdvice 接口(前置日志)
method:要执行的目标对象的方法
args:参数
target:目标对象
- package com.demo.log;
-
- import org.springframework.aop.MethodBeforeAdvice;
-
- import java.lang.reflect.Method;
-
- public class Log implements MethodBeforeAdvice {
-
- /*
- method:要执行的目标对象的方法
- args:参数
- target:目标对象
- */
- @Override
- public void before(Method method, Object[] args, Object target) throws Throwable {
- System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
- }
- }

再写个 AfterLog 类来实现 AfterReturningAdvice 接口(后置日志)
returnValue 返回值
- package com.demo.log;
-
- import org.springframework.aop.AfterReturningAdvice;
-
- import java.lang.reflect.Method;
-
- public class AfterLog implements AfterReturningAdvice {
-
- //returnValue返回值
- @Override
- public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
- System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
- }
- }
配置 applicationContext.xml 文件
注册 bean
配置 aop:需要导入 aop 的约束
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
使用原生 Spring API 接口
<aop:config>
切入点:expression:表达式,execution(要执行的位置:修饰词 返回值 类名 方法名 参数)
<aop:pointcut id="xx" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
执行环绕增加
<aop:advisor advice-ref="xx" pointcut-ref="xx"/>
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:c="http://www.springframework.org/schema/c"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd">
-
- <!-- 注册bean -->
- <bean id="user" class="com.demo.service.UserServiceImpl"/>
- <bean id="log" class="com.demo.log.Log"/>
- <bean id="afterLog" class="com.demo.log.AfterLog"/>
-
- <!-- 使用原生Spring API接口 -->
- <!-- 配置aop:需要导入aop的约束 -->
- <aop:config>
- <!-- 切入点:expression:表达式,execution(要执行的位置:修饰词 返回值 类名 方法名 参数) -->
- <aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
-
- <!-- 执行环绕增加 -->
- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
- </aop:config>
-
- </beans>

写个测试类
动态代理,代理的是接口
- import com.demo.service.UserService;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- //动态代理,代理的是接口
- UserService userService = (UserService) context.getBean("user");
- userService.add();
- }
- }
执行结果如下:
方法二:自定义类来实现 AOP(切面定义)
增加一个自定义的类
- package com.demo.pointcut;
-
- public class PointCut {
- public void before(){
- System.out.println("方法执行前");
- }
-
- public void after(){
- 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:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:c="http://www.springframework.org/schema/c"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd">
-
- <!-- 注册bean -->
- <bean id="user" class="com.demo.service.UserServiceImpl"/>
- <bean id="log" class="com.demo.log.Log"/>
- <bean id="afterLog" class="com.demo.log.AfterLog"/>
-
- <!-- 自定义类 -->
- <bean id="diy" class="com.demo.pointcut.PointCut"/>
-
- <aop:config>
- <!-- 自定义切面,ref:要引用的类 -->
- <aop:aspect ref="diy">
- <!-- 切入点 -->
- <aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
- <!-- 通知 -->
- <aop:before method="before" pointcut-ref="pointcut"/>
- <aop:after method="after" pointcut-ref="pointcut"/>
- </aop:aspect>
- </aop:config>
-
- </beans>

自定义切面,ref:要引用的类
<aop:aspect ref="xx">
切入点:
<aop:pointcut id="pointcut" expression="execution(* com.demo.service.UserServiceImpl.*(..))"/>
通知:
<aop:before method="xx" pointcut-ref="xx"/>
测试类不变,执行结果如下:
方法三:使用注解实现 AOP
注意导入包不要导错
- package com.demo.pointcut;
-
- //使用注解方式实现AOP
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.Signature;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
-
- @Aspect //标注这个类是一个切面
- public class AnnotationPointCut {
-
- @Before("execution(* com.demo.service.UserServiceImpl.*(..))")
- public void before(){
- System.out.println("方法执行前");
- }
-
- @After("execution(* com.demo.service.UserServiceImpl.*(..))")
- public void after(){
- System.out.println("方法执行后");
- }
-
- //在环绕增强中,可以给定一个参数,代表要获取处理切入的点
- @Around("execution(* com.demo.service.UserServiceImpl.*(..))")
- public void around(ProceedingJoinPoint joinPoint) throws Throwable {
- System.out.println("环绕前");
-
- Signature signature = joinPoint.getSignature(); //获得签名
- System.out.println(signature);
-
- //执行方法
- Object proceed = joinPoint.proceed();
-
- System.out.println("环绕后");
-
- System.out.println(proceed);
- }
- }

开启注解支持:<aop:aspectj-autoproxy/>
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:c="http://www.springframework.org/schema/c"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd">
-
- <!-- 注册bean -->
- <bean id="user" class="com.demo.service.UserServiceImpl"/>
- <bean id="log" class="com.demo.log.Log"/>
- <bean id="afterLog" class="com.demo.log.AfterLog"/>
-
- <bean id="anno" class="com.demo.pointcut.AnnotationPointCut"/>
- <!-- 开启注解支持 -->
- <aop:aspectj-autoproxy/>
-
- </beans>

测试类不变,执行结果如下:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。