一.通过注释实现切面编程
工程结构如下:
1.切面类Log.java,前置通知和后置通知以及环绕通知都在这里配置
package com.bijian.study.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; 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 Log { /*** * 配置切入点,该方法无方法体 * 注意切入申明的格式,execution(Integer(返回的类型) com.bijian.study.service.UserService.add(String)) */ @Pointcut("execution(* com.bijian.study.service..ad*(..))")//申明切人点,一个切入点会有很多的连接点 public void aspectMethod(){};//配置切入点,该方法无方法体 /*** * 配置前置通知 * @param joinpoint */ @Before("aspectMethod()") public void dddbefore1(JoinPoint joinPoint){ System.out.println("before添加日志"+joinPoint); } /*** * 配置后置通知 * @param joinpoint */ @After("aspectMethod()") public void after(){ System.out.println("after添加日志"); } /*** * 配置环绕通知 * @param joinpoint */ @Around("aspectMethod()") public void around(JoinPoint joinPoint){ System.out.println("开始around添加日志"); ProceedingJoinPoint pjp=(ProceedingJoinPoint)joinPoint; try { pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("结束around添加日志"); } }2.业务接口类UserService.java
package com.bijian.study.service; public interface UserService { public Integer add(String abc); public void aduser(String abc); }3.业务接口实现类UserServiceImpl.java
package com.bijian.study.service.impl; import org.springframework.stereotype.Service; import com.bijian.study.service.UserService; @Service public class UserServiceImpl implements UserService { @Override public Integer add(String abc) { System.out.println("添加用户信息1。"+abc); return 123; } @Override public void aduser(String abc) { System.out.println("添加用户信息2。"); } }4.配置文件bean.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com"/> <!-- 采用了注释实现自动装配实现代理 --> <aop:aspectj-autoproxy/> </beans>5.测试类AopTest.java
package com.bijian.study.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bijian.study.service.UserService; public class AopTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) ac.getBean("userServiceImpl"); userService.add("123oscar"); // userService.aduser("456"); } }运行结果:
开始around添加日志 before添加日志execution(Integer com.bijian.study.service.UserService.add(String)) 添加用户信息1。123oscar 结束around添加日志 after添加日志
6.说明
如果不采用spring的配置文件以及注释来申明的话,就需要实现很多的类。
比如:
通知类-用于说明切面类具有的功能.MethodBeforeAdvice
切面类-用于说明具体的需要切入的功能,这个是重点要做的,比如添加日志的功能就在这里log。
代理对象-用于申明要往哪里去切入日志功能的业务逻辑类proxyFactory。
被代理对象-具体说明业务逻辑模块。比如Userserviceimpl 用于实现添加用户等功能。
二.通过aop配置文件来申明切面类,不用注释
工程结构如下:
beans.xml配置文件,首先申明了切面类,然后申明了业务逻辑类,然后申明了AOP配置,配置切点和切面以及切入的方法。
1.配置文件bean.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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- <context:component-scan base-package="com" /> <aop:aspectj-autoproxy/>--> <!-- 切面类 --> <bean id="log" class="com.bijian.study.aop.Log"></bean> <!-- 用户管理 --> <bean id="userService" class="com.bijian.study.service.impl.UserServiceImpl"></bean> <!-- aop --> <aop:config> <!-- 切点 --> <aop:pointcut expression="execution(* com.bijian.study.service..*.*(..))" id="pointcut"/> <aop:aspect ref="log"> <!-- 通知 --> <aop:before method="dddbefore1" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>
2.去掉注释的切面类Log.java
package com.bijian.study.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class Log { /*** * 配置前置通知 * * @param joinpoint */ public void dddbefore1(JoinPoint joinPoint) { System.out.println("before添加日志:" + joinPoint); System.out.println("joinPoint.getTarget():" + joinPoint.getTarget()); } /*** * 配置后置通知 * @param joinpoint */ public void after() { System.out.println("after添加日志"); } /*** * 配置环绕通知 * @param joinpoint */ public void around(JoinPoint joinPoint) { System.out.println("开始around添加日志"); ProceedingJoinPoint pjp = (ProceedingJoinPoint) joinPoint; try { pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("结束around添加日志"); } }
3.业务接口类UserService.java
package com.bijian.study.service; public interface UserService { public Integer add(String abc); public void aduser(String abc); }
4.业务接口实现类UserServiceImpl.java
package com.bijian.study.service.impl; import com.bijian.study.service.UserService; public class UserServiceImpl implements UserService { @Override public Integer add(String abc) { System.out.println("添加用户信息1。"+abc); return 123; } @Override public void aduser(String abc) { System.out.println("添加用户信息2。"); } }
5.测试类AopTest.java
package com.bijian.study.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bijian.study.service.UserService; public class AopTest { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.add("123oscar"); // userService.aduser("456"); } }
运行结果:
before添加日志:execution(Integer com.bijian.study.service.UserService.add(String)) joinPoint.getTarget():com.bijian.study.service.impl.UserServiceImpl@5762806e 添加用户信息1。123oscar
相关推荐
实现spring的aop的操作,采用AspectJ技术,通过xml的配置来实现,本人亲自测试过,aop相关架包已引入
Spring-aop面向切面编程实例 日志系统,权限控制等.
spring AOP的代码实例,了解面向切面编程思想的精髓,
包内是aop入门的一些小例子。包括java代码和其中用到的xml文件。一个子包(如aop包)里有一个可执行应用程序。
想要跑起来,项目中,你要: (1)设置本地Maven的路径指向 (2)安装ssm-parent到本地仓库 (3)删除ssm-web-test(指向的Location可能和你的电脑磁盘路径不一致) (4)重新导入import 已经存在的 Maven ...
SpringAOP的注解配置完成切面的编程,完成execution,annotation两种表达式的实例Ddemo
Spring4AOP 面向切面编程实例之方法拦截实例 一下利用Spring4的最后一个版本Spring4.3.9,实现简单的方法拦截实例。 Eclipse 建立java工程,导入必要的jar包,工程目录如下:
spring 切面编程实例 aop spring 切面编程实例 aop
spring编程, AOP方面的.挺实用的
学习面向切面编程的起步实例。spring aop实例很好很实用。
aop写一个简单的计算器:(1)日志功能:在程序执行期间追踪正在发生的活动(打印出调用的方法,以及参数的参数值);...利用简单的实例来理解spring的aop编程,以aspectJ为例,理解环绕通知的真正含义。
SpringAOP,禁止非法路径请求后台。面向切面编程思想。
这两种代理方式在Spring AOP中起到关键作用,用于实现横切关注点的切面编程。通过学习它们的原理和实际应用,您将能够更好地理解和利用Spring AOP来提高您的应用程序的可维护性和可扩展性。 内容亮点: JDK动态...
SpringBoot之AOP面向切面编程实例
Spring 中的Aop面向切面编程详解,具体实例。
我先从框架底层模型的角度描述该框架的功能,然后将讨论两个最有趣的模块:Spring 面向方面编程(AOP)和控制反转 (IOC) 容器。接着将使用几个示例演示 IOC 容器在典型应用程序用例场景中的应用情况。这些示例还将...
采用注解模式,和XML模式对AOP面向切面编程进行测试。