? 面向切面编程(AOP)提供了另一种思考程序结构的方式来补充面向对象编程(OOP)。 OOP 中模块化的单元是类,而在 AOP 中模块化是切面。切面使关注点(例如事务 Management)的模块化可以跨越多种类和对象。
? Spring 的关键组件之一是 AOP 框架。尽管 Spring IoC 容器不依赖于 AOP(这意味着您不需要使用 AOP),但 AOP 是对 Spring IoC 的补充,以提供功能非常强大的中间件解决方案。
? AOP 要达到的效果是,保证开发者不修改源代码的前提下,去为系统中的业务组件添加某种通用功能。AOP 的本质是由 AOP 框架修改业务组件的多个方法的源代码,AOP 其实就是代理模式的典型应用。
? 按照 AOP 框架修改源代码的时机,可以将其分为两类:
AOP编程的相关概念很多,主要如下:
通俗的理解如下:
package pojo;
public interface Buy {
void buy();
}
package pojo;
public class Boy implements Buy{
@Override
public void buy() {
System.out.println("男的买了游戏机!!!!");
}
public void play(){
System.out.println("打游戏");
}
}
package pojo;
public class Girl implements Buy{
@Override
public void buy() {
System.out.println("女孩买了衣服");
}
public void guangjie(){
System.out.println("guangjie");
}
}
package configrution;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import pojo.Boy;
import pojo.Girl;
@Configuration
public class BuyConfigruation {
@Bean
public Boy boy(){
return new Boy();
}
@Bean
public Girl girl(){
return new Girl();
}
}
import configrution.BuyConfigruation;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pojo.Boy;
import pojo.Girl;
public class SpringTest {
@Test
public void springTest01(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BuyConfigruation.class);
Boy boy = applicationContext.getBean("boy", Boy.class);
Girl girl = applicationContext.getBean("girl", Girl.class);
boy.buy();
boy.play();
girl.buy();
girl.guangjie();
}
}
结果:
男的买了游戏机!!!!
打游戏
女孩买了衣服
guangjie
package configrution;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class BuyAspectJ {
@Pointcut("execution(* *.buy(..))")
public void point1() {
}
@Pointcut("execution(* *.guangjie(..))")
public void point2(){
}
@Pointcut("execution(* *.play(..))")
public void point3(){
}
@Before("point1()")
public void enhancementMethod(){
System.out.println("在买之前执行");
}
@After("point1()")
public void enhancementMethodAfter(){
System.out.println("买完了");
}
@Before("point2()")
public void enhancement(){
System.out.println("开机");
}
@After("point2()")
public void enhancement1(){
System.out.println("关机");
}
@Before("point3()")
public void enhancement2(){
System.out.println("化妆");
}
@After("point3()")
public void enhancement3(){
System.out.println("zhenlei");
}
}
package configrution;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import pojo.Boy;
import pojo.Girl;
@Configuration
@ComponentScan(basePackageClasses = {BuyAspectJ.class}) //让这个类的所有注解生效
@EnableAspectJAutoProxy(proxyTargetClass = true) // 开启AOP
public class BuyConfigruation {
@Bean
public Boy boy(){
return new Boy();
}
@Bean
public Girl girl(){
return new Girl();
}
}
结果:
在买之前执行
男的买了游戏机!!!!
买完了
化妆
打游戏
zhenlei
在买之前执行
女孩买了衣服
买完了
开机
guangjie
关机
AOP,
原文:https://www.cnblogs.com/mjoe/p/14523221.html