这篇文章主要为大家展示了“Java SpringAOP技术中注解方式是什么”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java SpringAOP技术中注解方式是什么”这篇文章吧。
<?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.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.xsd"> <!--开启注解扫描--> <context:component-scan base-package="com.qcby"></context:component-scan> </beans>
package com.qcby; import org.springframework.stereotype.Component; @Component(value = "user") public class User { //连接点/切入点 public void add(){ System.out.println("add......"); } }
给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明
@Component @Aspect //生成代理对象 public class UserProxy { }
<?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.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.xsd"> <!--开启扫描--> <context:component-scan base-package="com.qcby"></context:component-scan> <!--开启Aspect生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
@Before -- 前置通知
@AfterReturing -- 后置通知
@Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行)
@After -- 最终通知
@AfterThrowing -- 异常抛出通知
package com.qcby; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect //生成代理对象 public class UserProxy { //增强/通知 ---》前置通知 @Before(value = "execution(public void com.qcby.User.add())") public void before(){ System.out.println("前置通知............."); } // 环绕通知 @Around(value = "execution(public void com.qcby.User.add())") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕前置............."); // 执行被增强的方法 proceedingJoinPoint.proceed(); System.out.println("环绕后置............."); } // 最终通知 @After(value = "execution(public void com.qcby.User.add())") public void after() { System.out.println("最终通知............."); } //后置通知 @AfterReturning(value = "execution(public void com.qcby.User.add())") public void afterReturning() { System.out.println("后置通知............."); } //异常通知 @AfterThrowing(value = "execution(public void com.qcby.User.add())") public void afterThrowing() { System.out.println("出错了............."); } }
package com.qcby.test; import com.qcby.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { @Test public void aopTest1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringConfig.xml"); User user = (User) applicationContext.getBean("user"); user.add(); } }
以上是“Java SpringAOP技术中注解方式是什么”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。