这段时间,工作、个人都挺忙的,东西写了存在oneNote上面很久了...也没搬上来...
废话不多说了..
理论的东西,咱们先不说,代码优先,看下Spring AOP Demo样例。
package com.test.spring.server.test08;
/**
* Created by CYX on 2018/4/30.
*/
public interface HelloWorld {
void printHelloWorld();
void doPrint();
}
package com.test.spring.server.test08;
/**
* @author CYX
* @create 2018-04-30-22:06
*/
public class HelloWorldImpl1 implements HelloWorld {
@Override
public void printHelloWorld() {
System.out.println("HelloWorldImpl-1.printHelloWorld()");
}
@Override
public void doPrint() {
System.out.println("HelloWorldImpl-1.doPrint");
}
}
package com.test.spring.server.test08;
/**
* @author CYX
* @create 2018-04-30-22:08
*/
public class HelloWorldImpl2 implements HelloWorld {
@Override
public void printHelloWorld() {
System.out.println("HelloWorldImpl-2.printHelloWorld()");
}
@Override
public void doPrint() {
System.out.println("HelloWorldImpl-2.doPrint");
}
}
package com.test.spring.server.test08;
/**
* @author CYX
* @create 2018-04-30-22:08
*/
public class TimeHandler {
public void printTime() {
System.out.println("time : " + System.currentTimeMillis());
}
}
aop-applicationContext08.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:p="http://www.springframework.org/schema/p"
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"
xmlns:aop="http://www.springframework.org/schema/aop">
<bean id="helloWorldImpl1" class="com.test.spring.server.test08.HelloWorldImpl1"></bean>
<bean id="helloWorldImpl2" class="com.test.spring.server.test08.HelloWorldImpl2"></bean>
<bean id="timeHandler" class="com.test.spring.server.test08.TimeHandler"></bean>
<aop:config proxy-target-class="true">
<aop:aspect id="timeHandlerSpect" ref="timeHandler">
<aop:pointcut id="addAllMethod" expression="execution(* com.test.spring.server.test08.*.*(..))"/>
<aop:before method="printTime" pointcut-ref="addAllMethod"/>
<aop:after method="printTime" pointcut-ref="addAllMethod"/>
</aop:aspect>
</aop:config>
</beans>
main()
package com.test.spring.server.test08;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author CYX
* @create 2018-04-30-22:18
*/
public class Test08App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/spring/test08/aop-applicationContext08.xml");
HelloWorldImpl1 helloWorldImpl1 = context.getBean("helloWorldImpl1", HelloWorldImpl1.class);
HelloWorldImpl2 helloWorldImpl2 = context.getBean("helloWorldImpl2", HelloWorldImpl2.class);
System.out.println();
helloWorldImpl1.printHelloWorld();
helloWorldImpl1.doPrint();
System.out.println();
helloWorldImpl2.printHelloWorld();
helloWorldImpl2.doPrint();
}
}
运行结果:
上面就是最简单的AOP Demo代码。
咱们先别谈什么AOP的实现什么的...
通过上面,咱们先来想一个业务场景:
一个Web项目中有好多个Controller类(模块),业务要求,每次调用Controller(运行前、之后),都要进行某些安全校验、特定业务处理。
如果在每个Controller(模块)代码中都加上相应的代码、哪怕是函数提炼之后,那个感觉也不是太好。
如果我们使用AOP的话,就不用那么麻烦了。
下面看下 Spring AOP 的概念,虽然网上关于这些介绍多如牛毛,但为了完整,还是贴上。
Spring AOP
AOP:面向切面编程。在AOP中的基本单元是Aspect(切面)。
横切关注点:横切关注点可以被描述为影响应用多处的功能。
看下《Spring 实战》中的图
现在有CourseService、StudentService、MiscService三个Service服务。
每个服务调用的时候,都要关注 安全、事务等其他辅助功能。
如果是重用通用功能的话,比如继承、委托等。如果整个应用中都是用这些,会造成很多不优雅的地方。