天天看点

Spring整理复习(三)—Spring-AOP

概述:

Spring 框架的AOP 

         Sprign框架的一个关键组件是面对方面的编程(AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点。跨一个应用程序的多个功能被称为横切关注点。这些关注点在概念上独立于应用程序的业务逻辑。有各种各样的常见的很好方面的例子,如日志记录、审计、声明式事物、安全性和缓存。

 在OOP中,关键单元模块度是类,而在AOP中单元模块度是方面。依赖注入帮你对应用程序对象相互解耦和AOP可以帮助你从

 他们影响的对象中对横切关注点解耦。AOP是像编程语言的触发物。Perl,.NET,Java或其他

1.在Maven中导入相关jar包

<dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>3.2.8.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-beans</artifactId>
  		<version>3.2.8.RELEASE</version>
  	</dependency>
  	
  	<!-- 导入aspect -->
  	<dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjrt</artifactId>
    	<version>1.6.11</version>
	</dependency>
	<dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjweaver</artifactId>
    	<version>1.6.11</version>
	</dependency>
  </dependencies>
           

2.编写java代码

package cn.wl.test.entity;

public class Student {
	
	private String sname;
	
	private String sno;

	public String getSname() {
		System.out.println("Name:"+sname);
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getSno() {
		System.out.println("Sno:"+sno);
		return sno;
	}

	public void setSno(String sno) {
		this.sno = sno;
	}
	
	public void init() {
		System.out.println("Student实例的初始化");
	}
	
	public void destroy() {
		System.out.println("Student实例的销毁");
	}
	
	public  void printThrowException() {
		System.out.println("Exception  raised");
		throw new IllegalArgumentException();
		
	}
	
	
}
           
package cn.wl.test.entity.day06;

/**
 * Spring 框架的AOP
 * 
 * 		Sprign框架的一个关键组件是面对方面的编程(AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分
 * 	称为所谓的关注点。跨一个应用程序的多个功能被称为横切关注点。这些关注点在概念上独立于应用程序的业务逻辑。有
 * 各种各样的常见的很好方面的例子,如日志记录、审计、声明式事物、安全性和缓存。
 * 
 * 在OOP中,关键单元模块度是类,而在AOP中单元模块度是方面。依赖注入帮你对应用程序对象相互解耦和AOP可以帮助你从
 * 他们影响的对象中对横切关注点解耦。AOP是像编程语言的触发物。Perl,.NET,Java或其他
 * 
 * 
 * @author joker
 *
 */
public class Logging {

	/**
	 * 在一个方法中之前执行此方法
	 */
	public void beforeAdvice() {
		System.out.println("将要去执行student的方法");
	}
	
	/**
	 * 在一个方法之后执行此方法
	 */
	public void afterAdvice() {
		System.out.println("Student的方法己经被执行");
	}
	
	
	/**
	 * 在一个方法执行成功后执行此方法
	 * @param retVal
	 */
	public void afterReturningAdvice(Object retVal) {
		System.out.println("返回:"+retVal.toString());
	}
	
	
	/**
	 * 遇到异常时执行此方法
	 */
	public void afterThrowingAdvice(IllegalArgumentException ex) {
		System.out.println("这里有个异常:"+ex.toString());
	}
	
	
	
}
           
Spring整理复习(三)—Spring-AOP

3.编写配置文件

<?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:config>
      <aop:aspect id="log" ref="logging">
         <aop:pointcut id="selectAll" 
         expression="execution(* cn.wl.test.entity.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="afterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>
   
   	<bean id="student" class="cn.wl.test.entity.Student">
   		<property name="sno" value="010010"/>
   		<property name="sname" value="东方月初"/>
   	</bean>
   	<bean id="logging" class="cn.wl.test.entity.day06.Logging"/>
   
   
    
 
   
</beans>
           

4.测试

@Test
	public void test01() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans6.xml");
		Student student = ac.getBean("student",Student.class);
		student.getSname();
		student.getSno();
		student.printThrowException();
	}
/**
将要去执行student的方法
Name:东方月初
Student的方法己经被执行
返回:东方月初
将要去执行student的方法
Sno:010010
Student的方法己经被执行
返回:010010
将要去执行student的方法
Exception  raised
Student的方法己经被执行
这里有个异常:java.lang.IllegalArgumentException


*/
           

继续阅读