為了能夠使程序橫向擴(kuò)展,又不需要去改動(dòng)現(xiàn)有的程序,aop就出來(lái)了
舉個(gè)例子:必如說(shuō)我們把代碼都寫(xiě)完了,需要在某些功能執(zhí)行后打印一些日志看看,這時(shí)候你難道一個(gè)一個(gè)文件的去改嗎?aop就可以幫助我們直接切入,而不需要去改現(xiàn)有的文件,就像一把尖刀------>直入心臟
來(lái)先把依賴導(dǎo)了..
<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>com.wangjn</groupId>
<artifactId>spring-aop-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-aop-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.0.2.RELEASE</spring.version>
<!-- mybatis版本號(hào) -->
<mybatis.version>3.2.6</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring aop 編織依賴 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</project>
介紹一下配置aop的兩種方式:
第一種,使用 aop:aspect
首先,先建個(gè)接口
package com.wangjn;
import org.springframework.stereotype.Component;
/**
* Created by Administrator
*/
public interface MyAopDemo {
public void aopTest();
}
實(shí)現(xiàn)類:
package com.wangjn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by Administrator
*/
@Component
public class MyAopDemoImpl implements MyAopDemo{
private static final Logger logger = LoggerFactory.getLogger(MyAopDemoImpl.class);
@Override
public void aopTest() {
logger.info("Hello World!");
}
}
寫(xiě)個(gè) 橫切點(diǎn),也就是我們要切進(jìn)的具體執(zhí)行類:
package com.wangjn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by Administrator
*/
@Component("aspectTest")
public class AspectTest {
private static final Logger logger = LoggerFactory.getLogger(MyAopDemoImpl.class);
public void doit(){
logger.info("使用aspect 配置的AOP!");
}
}
重點(diǎn)在于spring的配置,其實(shí)就幾句話:
<?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-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:component-scan base-package="com"/>
<aop:config>
<aop:aspect id="log" ref="aspectTest">
<aop:pointcut id="addAllMethod" expression="execution(* com.wangjn.MyAopDemo.*(..))" />
<aop:before method="doit" pointcut-ref="addAllMethod" />
<aop:after method="doit" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans>
再來(lái)寫(xiě)個(gè)測(cè)試類:
package com.wangjn;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator
*/
public class Start {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MyAopDemo demo = (MyAopDemo) context.getBean("myAopDemoImpl");
demo.aopTest();
}
}
執(zhí)行 main 方法,完美切入..
再來(lái)說(shuō)說(shuō)使用advisor的aop 實(shí)現(xiàn),其實(shí)與上面的方法最大的不同在于需要自己去實(shí)現(xiàn)各種 advice(通知)
advice 類型有以下幾種:
- 前置(BeforeAdvice):在目標(biāo)方法執(zhí)行前實(shí)施
- 后置(AfterReturningAdvice):在目標(biāo)方法執(zhí)行后實(shí)施
- 環(huán)繞(MrthodInterceptor):在目標(biāo)方法執(zhí)行前后實(shí)施
- 異常拋出(ThrowsAdvice):在目標(biāo)方法拋出異常后實(shí)施
- 引介(IntroductionIntercrptor):在目標(biāo)類中添加一些新的方法和屬性
這里我們?nèi)?shí)現(xiàn)一下 AfterReturningAdvice 也就是 目標(biāo)方法后執(zhí)行
package com.wangjn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* Created by Administrator
*/
@Component
public class AdviceTest implements AfterReturningAdvice{
private static final Logger logger = LoggerFactory.getLogger(AdviceTest.class);
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
logger.info("使用advisor配置的AOP!");
}
}
改一下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-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<context:component-scan base-package="com"/>
<!-- 使用aspect 實(shí)現(xiàn)aop 與下面 advisor 配置的兩者選其一(事務(wù)配置一般用advisor的)
<aop:config>
<aop:aspect id="log" ref="aspectTest">
<aop:pointcut id="addAllMethod" expression="execution(* com.wangjn.MyAopDemo.*(..))" />
<aop:before method="doit" pointcut-ref="addAllMethod" />
<aop:after method="doit" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
-->
<aop:config>
<aop:pointcut id="addAllMethod" expression="execution(* com.wangjn.MyAopDemo.*(..))" />
<aop:advisor advice-ref="adviceTest" pointcut-ref="addAllMethod" ></aop:advisor>
</aop:config>
</beans>
執(zhí)行測(cè)試一下成功織入

image.png