SpringAop简单实践(基于XML)
<!--
基于XML的AOP配置
1)导入AOP相关坐标
2)创建目标接口和目标类(内部有切点)
3)创建切面类(内部有增强方法)
4)将目标类和切面类得对象创建权交给Spring
5)在applicationContext.xml中配置织入关系
6)测试代码
-->
1.1 Maven先引入包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_aop_test20210303</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- spring版本号 --> <spring.version>5.2.4.RELEASE</spring.version> <!-- junit版本号 --> <junit.version>4.13.2</junit.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>
1.2 准备目标类
package com.aop.xml.impl; import com.aop.xml.Target; public class TargetImpl implements Target { public void save() { System.out.println("save..."); } }
1.3 准备切面类
package com.aop.xml.impl; import com.aop.xml.MyAspect; import org.aspectj.lang.ProceedingJoinPoint; public class MyAspectImpl implements MyAspect { public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环绕前"); Object proceed = pjp.proceed(); System.out.println("环绕后"); return proceed; } public void before() { System.out.println("before"); } public void after() { System.out.println("after"); } public void afterReturing() { System.out.println("afterReturing"); } public void throwing() { System.out.println("throwing"); } }
1.4 新建applicationContext.xml
下面用到的切点表达式请关注这篇:https://www.cnblogs.com/year12/p/14469495.html
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="target" class="com.aop.xml.impl.TargetImpl"/> <bean id="myAspect" class="com.aop.xml.impl.MyAspectImpl"/> <aop:config> <aop:aspect ref="myAspect"> <aop:pointcut id="myPointcut" expression="execution(public * com.aop.xml..*.*(..))"/> <aop:after method="after" pointcut-ref="myPointcut"/> <aop:around method="around" pointcut-ref="myPointcut"/> </aop:aspect> </aop:config> <aop:aspectj-autoproxy/> </beans>
1.5 Spring整合Junit
package com.test.xml; import com.aop.xml.Target; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class JunitTestXml { @Autowired private Target target; @Test public void test(){ target.save(); } }
Nice to see you all!