要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间,此外我们还需要启动对@AspectJ注解的支持:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 启动对@AspectJ注解的支持 --> <aop:aspectj-autoproxy /> </beans>
PersonService接口
package org.spring.service; public interface PersonService { public void save(String name); public void update(String name, Integer id); public String getPersonName(Integer id); }
PersonService的实现类PersonServiceBean
package org.spring.service.impl; import org.spring.service.PersonService; public class PersonServiceBean implements PersonService { public String getPersonName(Integer id) { System.out.println("我是getPersonName()方法"); return "xxx"; } public void save(String name) { System.out.println("我是save()方法"); } public void update(String name, Integer id) { System.out.println("我是update()方法"); } }
切面
package org.spring.service; 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; @Aspect public class MyInterceptor { // 拦截org.spring.service.impl.PersonServiceBean下返回值为任意类型的所有方法,参数可有可无,个数不限定 @Pointcut("execution (* org.spring.service.impl.PersonServiceBean.*(..))") private void anyMethod() { }// 声明一个切入点,用一个方法表示 @Before("anyMethod() && args(name)") public void doAccessCheck(String name) { System.out.println("前置通知:" + name); } // 返回值类型为字符串 @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 doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { // if(){//判断用户是否在权限 System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); // } return result; } }
我们还需要把PersonServiceBean与MyInterceptor交于Spring容器管理,配置文件修改如下
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 启动对@AspectJ注解的支持 --> <aop:aspectj-autoproxy /> <bean id="myInterceptor" class="org.spring.service.MyInterceptor" /> <bean id="personServiceBean" class="org.spring.service.impl.PersonServiceBean" /> </beans>
测试类
package org.spring.junit; import org.junit.Test; import org.spring.service.PersonService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring.xml"); PersonService personService = (PersonService) ctx .getBean("personServiceBean"); personService.save("xxx"); } }
控制台打印结果为:
前置通知:xxx
进入方法
我是save()方法
后置通知:null
最终通知
退出方法
由于save方法没有返回值,故后置通知中的result为null
相关推荐
使用Spring的注解方式实现AOP的细节
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
使用Spring的注解方式实现AOP入门
NULL 博文链接:https://88548886.iteye.com/blog/1571102
spring注解方式实现aop,希望对大家有所帮助,内附注释
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
NULL 博文链接:https://sunbin.iteye.com/blog/2373014
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
Spring 使用AspectJ 实现 AOP(基于xml文件、基于注解)
演示了spring对aop的支持,包括注解方式、基于xml方式。
NULL 博文链接:https://huihai.iteye.com/blog/858769
本篇文章主要介绍了使用Spring的注解方式实现AOP实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring中实现AOP有很多方式,这里总结了四种,有基于代理的实现方式,也有基于AspectJ 的注解方式,还有基于AOP标签的配置
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
NULL 博文链接:https://coolszy.iteye.com/blog/540465
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
spring 注解 aop demo spring 注解 aop demo spring 注解 aop demo spring 注解 aop demo
在SpringBoot中演示实现了aop的使用方法,包括pointcut和Advice
2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、可在现有源码上快速进行功能扩展 4、spring boot,mybatis,druid,spring aop的使用