Spring AOP

1. 基本概念

AOP(Aspect Oriented Programming)面向切面编程,经过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术。AOP是OOP的补充,是spring框架中的一个重要内容。利用AOP能够对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度下降提升程序的可重用性,同时提升了开发的效率。java

2. 经过XML配置示例

代码组织结构图: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");
			
	}
}

3. 经过注解方式实现AOP

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");
			
	}
}
相关文章
相关标签/搜索