第一章:Spring AOP
Aop面向切面编程
-
新建一个空的 Java maven项目,命名为【spring01】
-
修改pom.xml文件,增加如下依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.5.RELEASE</spring.version> <aopalliance.version>1.0</aopalliance.version> <aspectj.weaver>1.9.2</aspectj.weaver> </properties> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</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.6.11</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.3</version> </dependency> </dependencies>
3.创建通知类
public class MyAspject { public void before() { System.out.println("前置通知"); } public Object around(ProceedingJoinPoint j) { Object proceed = null; try { System.out.println("前置通知"); proceed = j.proceed(); System.out.println("后置通知"); return proceed; } catch (Throwable throwable) { System.out.println("异常通知"); throwable.printStackTrace(); }finally{ System.out.println("最终通知"); } return proceed; } }
4.创建applicationContext配置文件
<?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:tx="http://www.springframework.org/schema/tx" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userDao" class="club.gskj.dao.impl.UserDaoImpl"></bean> <bean id="aspject" class="club.gskj.aspject.MyAspject"></bean>
<!--配置 切面-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="point" expression="execution(* club.gskj.dao..*.*(..))"/>
<!--切入点-->
<aop:aspect ref="aspject">
<aop:before method="before" pointcut-ref="point"/>
<aop:around method="around" pointcut-ref="point" arg-names="j"/>
</aop:aspect>
</aop:config>
</beans>
5.测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class AspectTest { @Autowired UserDao userDao; @Test public void teset1() { userDao.add(); } }
Aop面向切面编程,anno注解模式
1. 通知类
@Aspect @Component public class MyAspject2 { @Pointcut(value = "execution(* club.gskj.dao..*.*(..)))") public void pointCut() { } @Before(value = "pointCut()") public void before() { System.out.println("前置通知"); } @Around(value = "pointCut()") public Object around(ProceedingJoinPoint j) { Object proceed = null; try { System.out.println("前置通知 anno"); proceed = j.proceed(); System.out.println("后置通知 anno"); return proceed; } catch (Throwable throwable) { System.out.println("异常通知 anno"); throwable.printStackTrace(); } finally { System.out.println("最终通知 anno"); } return proceed; } }
2. applicationContext配置
<?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:tx="http://www.springframework.org/schema/tx"
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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userDao" class="club.gskj.dao.impl.UserDaoImpl"></bean>
<bean id="aspject" class="club.gskj.aspject.MyAspject"></bean>
<!--开启 aop 自动注解-->
<aop:aspectj-autoproxy/>
<!--开启扫描包-->
<context:component-scan base-package="club.gskj"/>
</beans>
3.测试类测试
-
总结:
-
aop面向切面编程。重点在于使用的是 动态代理模式。在不改变源代码的基础上,对方法进心再次加强