1. 程式人生 > >Spring學習——AOP切面

Spring學習——AOP切面

AOP面向切面程式設計(Aspect Oriented Programming),通過預編譯方式和執行期動態代理實現程式功能的統一維護的一種技術

可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率

AOP需要在beans.xml中單獨配置,包括<beans/>中的aop路徑和config配置標籤

1、通過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:config>
   		<!-- 定義切面 -->
   		<aop:aspect id="log" ref="logBean">
   			<!-- 定義連線點,掃描 com.spring.Aop.demo 下所有類的所有函式 並宣告id-->
   			<aop:pointcut expression="execution(* com.spring.Aop.demoXML.*.*(..))" id="point" />
   			
   			<!-- 呼叫之前執行函式 before-->
   			<aop:before method="before" pointcut-ref="point"/>
   			
   			<!-- 之後 -->
   			<aop:after method="after" pointcut-ref="point"/>
   			<!-- 
   				在目標方法完成之後,增強處理(會限制目標返回值)
   				returning="retVal"表明允許在方法afterReturning()中定義名為result的形參  
   			-->
   			<aop:after-returning method="afterReturning" pointcut-ref="point"  returning="result"/>
   			
   			
   		</aop:aspect>
   	</aop:config>
   	
	<bean id="user" class="com.spring.Aop.demoXML.User">
		<property name="username" value="admin"/>
		<property name="password" value="123456"/>    
	</bean>
	<bean id="logBean" class="com.spring.Aop.demoXML.Log"></bean>
	
</beans>

切面處理類

package com.spring.Aop.demoXML;

public class Log {
	public void before(){
		System.out.println("before之前");
	}
	public void after(){
		System.out.println("after之後");
	}
	
	public void afterReturning(Object result){
		System.out.println("afterReturning:"+result);
	}
	
	public void afterThrowing(Exception exception){
		System.out.println("afterThrowing:"+exception.toString());
	}
}

切面類

package com.spring.Aop.demoXML;

public class User {
	private String username;
	private String password;
	public String getUsername() {
		System.out.println("getUsername:"+username);
		return username;
	}
	public void setUsername(String username) {
		System.out.println("setUsername:"+username);
		this.username = username;
	}
	public String getPassword() {
		System.out.println("getPassword:"+password);
		return password;
	}
	public void setPassword(String password) {
		System.out.println("setPassword:"+password);
		this.password = password;
	}
	
}

主函式類

package com.spring.Aop.demoXML;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMain {
	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext("BeansXML.xml");
		User user = (User) context.getBean("user");
		System.out.println("-----------------------------");
		//User user = new User();
		user.getUsername();
		System.out.println("-----------------------------");
		user.getPassword();
		System.out.println("-----------------------------");
		user.setUsername("admin1");
		System.out.println("-----------------------------");
		user.setPassword("123451");
		System.out.println("-----------------------------");
		user.getUsername();
		System.out.println("-----------------------------");
		user.getPassword();
	}
}

2、使用註解配置

<?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:aspectj-autoproxy/>
   	
   	<bean id="user1" class="com.spring.Aop.demoAnnotation.User">
   		<property name="username" value="admin"/>
   		<property name="password" value="12345" />
   	</bean>
   	<bean id="log1" class="com.spring.Aop.demoAnnotation.Log"/>
  </beans>

log切面類

package com.spring.Aop.demoAnnotation;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Log {
	
	@Pointcut("execution(* com.spring.Aop.demoAnnotation.User.*(..))")
	public void logging(){System.out.println("pointcut切入點");}
	
	@Before("logging()")
	public void before(){
		System.out.println("before之前");
	}
	@After("logging()")
	public void after(){
		System.out.println("after之後");
	}
	@AfterReturning(pointcut="logging()", returning="result")
	public void afterReturning(Object result){
		System.out.println("afterReturning:"+result);
	}
}