AOP(Aspect Oriented Programming)面向切面編程,經過預編譯方式和運行期動態代理實現程序功能的橫向多模塊統一控制的一種技術。AOP是OOP的補充,是spring框架中的一個重要內容。利用AOP能夠對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度下降,提升程序的可重用性,同時提升了開發的效率。java
代碼組織結構圖:spring
pom.xmlexpress
<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.aop</groupId> <artifactId>aop1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> </dependencies> </project>
Logging.javaapache
package com.aop; public class Logging { /** * 在選定的方法執行前執行 * 參數:name */ public void beforeAdvive(String name){ System.out.println("前置通知======"+name); } /** * 在選定的方法執行後執行 */ public void afterAdvive(){ System.out.println("最終通知======"); } /** * 在任何方法返回後執行 */ public void afterReturningAdvive(){ System.out.println("afterReturningAdvive======"); } public void afterThrowingAdvive(){ System.out.println("afterThrowingAdvive======="); } }
Order.java編程
package com.aop; public class Order { private String name; private Double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public void printThrowException(){ System.out.println("Exception raised"); throw new IllegalArgumentException(); } }
Beans.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" 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 "> <bean id="order" class="com.aop.Order"></bean> <!-- aop配置 --> <aop:config> <!--切面 --> <aop:aspect id="log" ref="logging"> <!-- 切點 --> <aop:pointcut id="pointcut1" expression="execution(* com.aop.Order.setName(..)) and args(name)"/><!-- 帶參 切點--> <!--鏈接通知方法與切點 --> <aop:before pointcut-ref="pointcut1" method="beforeAdvive" arg-names="name"/><!-- 前置通知,傳遞參數 --> </aop:aspect> </aop:config> <!-- Definition for logging aspect --> <bean id="logging" class="com.aop.Logging"/> </beans>
測試代碼:maven
package com.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TsMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Order order = (Order) context.getBean("order"); order.setName("limh"); } }
Logging.java測試
package com.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; /** * 不要漏了@Aspect註解 */ @Aspect public class Logging { /** * 切點 */ @Pointcut("execution(* com.aop.Order.setName(..)) and args(name)") public void pointcut1(){} /** * 在選定的方法執行前執行 * 傳參格式:args(name) */ @Before("pointcut1() && args(name)") public void beforeAdvive(String name){ System.out.println("前置通知======"+name); } }
Order.javathis
package com.aop; public class Order { private String name; private Double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public void printThrowException(){ System.out.println("Exception raised"); throw new IllegalArgumentException(); } }
Beans.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" 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 "> <!-- 必須有這個aop註解,指明項目中用到aop --> <aop:aspectj-autoproxy/> <bean id="order" class="com.aop.Order"></bean> <!-- Definition for logging aspect --> <bean id="logging" class="com.aop.Logging"/> </beans>
測試代碼:
package com.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TsMain { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Order order = (Order) context.getBean("order"); order.setName("limh"); } }