開始使用
在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期動態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點(diǎn),也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
系統(tǒng)要求:
本文采用Java 1.8.0_102、Spring Boot 1.5.9調(diào)試通過。
使用Maven構(gòu)建項(xiàng)目
- 通過SPRING INITIALIZR工具產(chǎn)生基礎(chǔ)項(xiàng)目
- 訪問:http://start.spring.io/
- 選擇構(gòu)建工具Maven Project、Spring Boot版本1.5.9以及一些工程基本信息
點(diǎn)擊Generate Project下載項(xiàng)目壓縮包
- 解壓項(xiàng)目包,并用IDE以Maven項(xiàng)目導(dǎo)入,以IntelliJ IDEA 14為例:
- 菜單中選擇File–>New–>Project from Existing Sources...
- 選擇解壓后的項(xiàng)目文件夾,點(diǎn)擊OK
- 點(diǎn)擊Import project from external model并選擇Maven,點(diǎn)擊Next到底為止。
- 若你的環(huán)境有多個版本的JDK,注意到選擇Java SDK的時候請選擇Java 7以上的版本
項(xiàng)目結(jié)構(gòu)解析
通過上面步驟完成了基礎(chǔ)項(xiàng)目的創(chuàng)建,Spring Boot的基礎(chǔ)結(jié)構(gòu)共三個文件(具體路徑根據(jù)用戶生成項(xiàng)目時填寫的Group所有差異):
- src/main/java下的程序入口:Application
- src/main/resources下的配置文件:application.properties
- src/test/下的測試入口:ApplicationTests
引入AOP模塊
引入aop模塊,需添加spring-boot-starter-aop模塊:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
編寫Hello服務(wù)
- 創(chuàng)建package命名為com.learn.service(根據(jù)實(shí)際情況修改)
- 創(chuàng)建接口HelloService,內(nèi)容如下
public interface HelloService {
void sayHello();
}
- 創(chuàng)建package命名為com.learn.service.impl(根據(jù)實(shí)際情況修改)
- 創(chuàng)建實(shí)現(xiàn)類HelloServiceImpl,內(nèi)容如下
@Service
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello() {
System.out.println("hello aop");
}
}
編寫單元測試用例
打開的src/test/下的測試入口ApplicationTests類。下面編寫一個簡單的單元測試,具體如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Resource
private HelloService helloService;
@Test
public void test() {
helloService.sayHello();
}
}
運(yùn)行可以看到結(jié)果:
hello aop
使用aop
- 創(chuàng)建package命名為com.learn.aop(根據(jù)實(shí)際情況修改)
- 創(chuàng)建類HelloServiceAop,內(nèi)容如下
@Aspect
@Component
public class HelloServiceAop {
//匹配com.learn.service.impl包及其子包下的所有類的所有方法
@Pointcut("execution(* com.learn.service.impl..*.*(..))")
public void executeService(){
}
@Before("executeService()")
public void doBeforeAdvice(JoinPoint joinPoint){
System.out.println("我是前置通知!!!");
//獲取目標(biāo)方法的參數(shù)信息
Object[] obj = joinPoint.getArgs();
System.out.println(obj);
//AOP代理類的信息
joinPoint.getThis();
//代理的目標(biāo)對象
joinPoint.getTarget();
//用的最多 通知的簽名
Signature signature = joinPoint.getSignature();
//代理的是哪一個方法
System.out.println(signature.getName());
//AOP代理類的名字
System.out.println(signature.getDeclaringTypeName());
//AOP代理類的類(class)信息
System.out.println(signature.getDeclaringType());
}
}
運(yùn)行之前的測試用例,可以看到輸出已經(jīng)變成了:
我是前置通知!!!
[Ljava.lang.Object;@997d532
sayHello
com.learn.service.HelloService
interface com.learn.service.HelloService
hello aop
可以看到在方法前面加上了一系列附加信息
當(dāng)然,通過AOP不僅僅只能在方法前面加上一些額外處理,你還可以在方法后面,異常時等等進(jìn)行一些額外的處理。
在后面的教程中將會詳細(xì)說到這些。