完整代码在这php
AOP的全称是 Aspect Oriented Program(面向切面编程)java
按照个人理解,AOP的思想通俗易懂的来讲就是将程序的功能分为两类,一类是核心功能,另外一类是辅助功能。spring
在非面向切面编程里,核心功能和辅助功能老是耦合在一块儿,好比说进行数据库操做时,日志记录行为的代码老是紧挨着数据库操做(不知作别的大神是什么样,反正我是这样)。这样一来,核心功能(数据库操做)的代码就和辅助功能(记录日志)的代码耦合在一块儿了。数据库
而在AOP里,核心功能和辅助功能的代码是彻底分开的,它们能够各自独立修改,而彻底不干扰对方,这实际上应该是应用了Java设计模式里的代理模式(改天记得复习一下)。express
要加哪些包?apache
spring的核心包就不用说了,除此以外还要加上和AOP有关的包,aspectJ,完整的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>SpringLearn</groupId>
<artifactId>SpringDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>
</project>
复制代码
要加哪些类?设计模式
LoggerAspectbash
ProductServiceeclipse
Category
Product
目录结构以下:
建立一个核心功能的类 ProductService
package com.learn.service;
import org.springframework.stereotype.Component;
@Component("ps")
public class ProductService {
public void doSomeService() {
System.out.println("伪装这里是一些核心业务代码!");
}
}
复制代码
建立一个辅助功能的类 LoggerAspect
package com.learn.aspect;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
@Component("la")
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
复制代码
能够看到这里核心功能的代码和辅助功能的代码是彻底分开的,那么怎么把它们联系到一块儿呢?
这就须要配置Spring的配置文件了,具体配置以下:
<?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: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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描bean-->
<context:component-scan base-package="com.learn.service com.learn.springBasic com.learn.aspect"/>
<!--配置AOP-->
<aop:config>
<!--配置切点-->
<aop:pointcut id="loggerCutpoint" expression="execution(* com.learn.service.ProductService.*(..)) "/>
<!--配置织入对象-->
<aop:aspect id="logAspect" ref="la">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>
复制代码
配置文件中须要加入 <aop:config> 标签。
<aop:pointcut> 标签配置的是核心类,在这里指定了要织入的对象是哪一个。
execution(* com.learn.service.ProductService.*(..))
复制代码
上面这句表达式表示,全部ProductService下的方法都将被织入辅助功能,既在执行该方法时会调用辅助功能。 <aop:aspect> 配置的是辅助类
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
复制代码
上面标签中的 pointcut-ref 很显然就是要织入的核心类id, method 就是被织入的方法。
运行结果:
start log:doSomeService
伪装这里是一些核心业务代码!
end log:doSomeService
复制代码
运行结果呈现出这样的效果的是由于辅助类中log方法中是这样写的:
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
复制代码
log方法最终返回的是一个Object对象,就本例来讲,返回的其实是ProductService 对象,这里应该是用到了代理模式来实现。
注解方式配置AOP也很简单,和注解方式配置bean是很是类似的。
首先,spring配置文件中把 <aop:config> 下的标签都删掉,换成 <aop:aspectj-autoproxy/> 标签来启用自动代理。
<?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: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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描bean-->
<context:component-scan base-package="com.learn.service com.learn.springBasic com.learn.aspect"/>
<!--配置自动代理AOP-->
<aop:aspectj-autoproxy/>
</beans>
复制代码
LoggerAspect 类中也要作一些改变,加上@Aspect 和 @Around注解。
package com.learn.aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
@Aspect
@Component("la")
public class LoggerAspect {
@Around(value = "execution(* com.learn.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
复制代码
运行结果:
start log:doSomeService
伪装这里是一些核心业务代码!
end log:doSomeService
复制代码