JAVA全系列 教程
3762个小节阅读:7095.1k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在Spring 框架中,AOP注解只支持AspectJ方式的注解。通过注解来简化AspectJ方式的XML配置。
我们可以通过XML+注解方式实现AOP,也可以使用纯注解方式实现AOP。
如果使用XML+注解方式实现AOP,需要编写applicationContext.xml配置文件,开启注解扫描和启用AOP注解
xxxxxxxxxx
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置注解扫描路径:-->
<context:component-scan base-package="com.bjsxt.annotation"> </context:component-scan>
<!--配置AOP注解生效-->
<aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>
</beans>
然后在测试类中,使用locations属性加载配置文件
xxxxxxxxxx
@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class AnnotationTest {
}
如果使用纯注解方式,其实就是使用Java Config方式,把applicationContext.xml替换为配置类
xxxxxxxxxx
@Configuration
// 等效于<context:component-scan base-package="com.bjsxt.annotation">
@ComponentScan("com.bjsxt.annotation")
// 等效于<aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>
@EnableAspectJAutoProxy
public class AnnotationConfig {
}
然后在测试类中,使用classes属性加载配置类
xxxxxxxxxx
@SpringJUnitConfig(classes = AnnotationConfig.class)
public class AnnotationTest {
}