Java SpringAOP技术之注解方式详解
短信预约 -IT技能 免费直播动态提醒
1.配置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.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>
2.配置注解
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 {
}
3.配置文件中开启自动代理
<?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>
4.通知类型注解
@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("出错了.............");
}
}
5.测试类
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();
}
}
6.结果
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341