AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP
代码
maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
package cn.why.test1.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* Title: LoggerAspect
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
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;
}
}
package cn.why.test1.pojo;
/**
* Title: Category
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package cn.why.test1.pojo;
/**
* Title: Producgt
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
package cn.why.test1.service;
/**
* Title: ProductService
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
package cn.why.test1.test;
import cn.why.test1.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Title: TestSpring
* Description:
* Company: AceGear
* Author: henrywang
* Date: 2018/8/2
* JDK: 8
* Encoding: UTF-8
*/
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext2.xml"});
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}
applicationContext2.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="cn.why.test1.pojo.Category">
<property name="name" value="yyy"/>
</bean>
<bean name="p" class="cn.why.test1.pojo.Product">
<property name="name" value="product1"/>
<property name="category" ref="c"/>
</bean>
<bean name="s" class="cn.why.test1.service.ProductService"/>
<bean id = "loggerAspect" class="cn.why.test1.aspect.LoggerAspect"/>
<aop:config>
<aop:pointcut id="loggerCutpoint" expression="execution(* cn.why.test1.service.ProductService.*(..))"/>
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around method="log" pointcut-ref="loggerCutpoint"/>
</aop:aspect>
</aop:config>
</beans>
整体结构