What is AOP:
In the software industry, AOP is the abbreviation for Aspect Oriented Programming, meaning: Aspect Oriented Programming , by pre-compiled manner of uniform maintenance and operation of dynamic proxy implementation program features a technology. AOP is OOP continuation, is a hot spot in software development, but also Spring an important element of the framework is the functional programming a Yansheng Fan type. AOP can use to isolate each part of the business logic such that the business logic portions between coupling reduction, improve the reusability of the program, while improving efficiency of development. During the program runs, the method of functional enhancements without modifying source code.
Related concepts:
Target (Audience): proxy target object
The Proxy (Agent): after being woven into a class enhanced AOP, to produce a result proxy class
JoinPoint (connection points): a so-called connection points are those points being intercepted. In the spring, these points refers to a method, because only the spring supporting method junction type
Pointcut (entry point): The so-called entry points means we want to define what Joinpoint intercept, enhanced methods
Advice (notification / Enhanced): The so-called notification means to intercept Joinpoint have to do is to inform, enhance business logic encapsulation method
Aspect (section): the entry point and the notification (introducing) binding tangent point notification +
Weaving (weaving): it refers to the enhancement applied to the target object to the process of creating a new proxy object. Agent dynamic spring weaving, while the weaving AspectJ use of compiled class loading procedure and of weaving, combined with the tangent point of the notification
Role: during program execution, the method of functional enhancements without modifying the source code
Advantages: reduce code duplication, improve development efficiency, and easy to maintain
AOP dynamic agent technology:
The methods of dynamic agent technology
JDK Agent: Dynamic agent technology based interface
cglib Agent: Dynamic agent technology based parent
Acting on their own to understand the dynamic.
AOP develop clear matters:
1) What to write:
-
Write core business code (target method of target class)
-
Section writing class, cut class has notice (enhancement method)
-
In the configuration file, configuration woven into the relationship, which notifies the connection point which is about to be combined
2) AOP technology content:
Spring monitor the implementation of the framework entry point method. Once the entry point monitoring method is run, using the proxy mechanism, dynamically created proxy object the target object, based on the notification type, at a position corresponding to the proxy object, the notification function corresponding weaving, complete the full code logic operation.
3) What kind of underlying AOP proxy mode use:
In the spring, the framework will be based on whether the target class implements the interface to determine which dynamic proxy.
4) develop clear matters:
Who is the cut point (cut point expression configuration)
Who is to inform (enhancement section class)
The tangent point and the weaving setup notification
AOP XML-based development
Getting Started:
① import AOP associated coordinates
1 <!--导入spring的context坐标,context依赖aop--> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-context</artifactId> 5 <version>5.0.5.RELEASE</version> 6 </dependency> 7 <!-- aspectj的织入 --> 8 <dependency> 9 <groupId>org.aspectj</groupId> 10 <artifactId>aspectjweaver</artifactId> 11 <version>1.8.13</version> 12 </dependency>
1 public interface TargetInterface { 2 public void method(); 3 } 4 5 public class Target implements TargetInterface { 6 @Override 7 public void method() { 8 System.out.println("Target running...."); 9 } 10 }
③创建切面类(内部有增强方法)
1 public class MyAspect { 2 //前置增强方法 3 public void before(){ 4 System.out.println("前置代码增强....."); 5 } 6 }
④将目标类和切面类的对象创建权交给 spring
<!--配置目标类--> <bean id="target" class="com.itheima.aop.Target"></bean> <!--配置切面类--> <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
⑤在 applicationContext.xml 中配置织入关系
<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/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 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <aop:config> <!--引用myAspect的Bean为切面对象--> <aop:aspect ref="myAspect"> <!--配置Target的method方法执行时要进行myAspect的before方法前置增强--> <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before> </aop:aspect> </aop:config> </bean>
⑥测试代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AopTest { @Autowired private TargetInterface target; @Test public void test1(){ target.method(); } }
XML 配置 AOP 详解:
切点表达式的写法
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
-
-
返回值类型、包名、类名、方法名可以使用星号* 代表任意
-
包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
-
参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
例如:
execution(public void com.itheima.aop.Target.method()) execution(void com.itheima.aop.Target.*(..)) execution(* com.itheima.aop.*.*(..)) execution(* com.itheima.aop..*.*(..)) execution(* *..*.*(..))
通知的类型
通知的配置语法:
<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>
切点表达式的抽取
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。
<aop:config> <!--引用myAspect的Bean为切面对象--> <aop:aspect ref="myAspect"> <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/> <aop:before method="before" pointcut-ref="myPointcut"></aop:before> </aop:aspect> </aop:config>
知识要点
-
aop织入的配置
<aop:config> <aop:aspect ref=“切面类”> <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before> </aop:aspect> </aop:config>
-
-
切点表达式的写法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
基于注解的 AOP 开发快速入门:
基于注解的 AOP 开发步骤:
①创建目标接口和目标类(内部有切点)
public interface TargetInterface { public void method(); } public class Target implements TargetInterface { @Override public void method() { System.out.println("Target running...."); } }
②创建切面类(内部有增强方法)
public class MyAspect { //前置增强方法 public void before(){ System.out.println("前置代码增强....."); } }
③将目标类和切面类的对象创建权交给 spring
@Component("target") public class Target implements TargetInterface { @Override public void method() { System.out.println("Target running...."); } } @Component("myAspect") public class MyAspect { public void before(){ System.out.println("前置代码增强....."); } }
④在切面类中使用注解配置织入关系
@Component("myAspect") @Aspect public class MyAspect { @Before("execution(* com.itheima.aop.*.*(..))") public void before(){ System.out.println("前置代码增强....."); } }
⑤在配置文件中开启组件扫描和 AOP 的自动代理
<!--组件扫描-->
<context:component-scan base-package="com.itheima.aop"/>
<!--aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
⑥测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AopTest { @Autowired private TargetInterface target; @Test public void test1(){ target.method(); } }
注解配置 AOP 详解
1) 注解通知的类型
通知的配置语法:@通知注解(“切点表达式")
同 xml配置 aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:
@Component("myAspect") @Aspect public class MyAspect { @Before("MyAspect.myPoint()") public void before(){ System.out.println("前置代码增强....."); } @Pointcut("execution(* com.itheima.aop.*.*(..))") public void myPoint(){} }