这篇文章主要为大家详细介绍了Java SpringAOP技术之注解方式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
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.结果
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程学习网的更多内容!
沃梦达教程
本文标题为:Java SpringAOP技术之注解方式详解
基础教程推荐
猜你喜欢
- Java实现线程插队的示例代码 2022-09-03
- Java文件管理操作的知识点整理 2023-05-19
- Java并发编程进阶之线程控制篇 2023-03-07
- java基础知识之FileInputStream流的使用 2023-08-11
- java实现多人聊天系统 2023-05-19
- Java实现查找文件和替换文件内容 2023-04-06
- springboot自定义starter方法及注解实例 2023-03-31
- Java数据结构之对象比较详解 2023-03-07
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- JDK数组阻塞队列源码深入分析总结 2023-04-18