首先有如下两个关于开发过程的概念:
- 纵向编程,代码较为重复
- 横向编程,重复代码形成切面织入节点
下面编写实例:
- 添加额外倚赖
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
- 新建类
public class BeforeAdvice {
public void methodBefore(){
System.out.println("我在方法之前执行");
}
}
public class ProviderService {
public void add(){
System.out.println("添加了一个供应商");
}
}
- 配置文件
<?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:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!--【第一步:激活代理】 aop是基于代理完成的,所以我们要激活我们的自动代理-->
<aop:aspectj-autoproxy/>
<!--【第二步:注册一个切面要使用的类】-->
<bean class="com.zt.advice.BeforeAdvice" id="beforeAdvice">
</bean>
<bean class="com.zt.service.ProviderService" id="providerService">
</bean>
<!--【第三步:配置切入点等等信息】-->
<aop:config >
<aop:aspect id="beforeAspect" ref="beforeAdvice">
<!--
aop:before 表明它是前置通知
method 指明了它是哪个方法来切
pointcut:切入点
*包下的*类*方法
-->
<aop:before method="methodBefore" pointcut="execution(* com.zt.service.ProviderService(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>
带张图解如下:
关于execution部分的解释如下:
4. 测试
@Test
public void m1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
ctx.getBean("beforeAdvice", BeforeAdvice.class);
//如果不是spring容器管理的bean,那么织入行为无法生效
// ProviderService providerService = new ProviderService();
/*ProviderService providerService = ctx.getBean("providerService", ProviderService.class);
providerService.add();*/
}