面向切面編程的例子


隨意的測試結構

//Aspect1.java
package entity;

public class Aspect1 {
		public void before(){
			System.out.println("業務執行前,before方法被執行");
		}
		
		public void after(){
			System.out.println("業務執行后,after方法被執行");
		}
}

//Service1.java

package service;

public class Service1 {

	public  void doSomething1(){
		System.out.println("業務服務1");
	}
	
	public  void doSomething2(){
		System.out.println("業務服務2");
	}

	public Service1() {

	}
	
	
}


//TestDemo .java

package test;

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

import service.Service1;

public class TestDemo {
	
	@Test
	public void test(){
		ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
		Service1 s=ac.getBean("s1",Service1.class);
		s.doSomething1();
		System.out.println();
		s.doSomething2();
	}

}

一:基於xml配置##

①applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<bean id="s1" class="service.Service1"></bean>
<bean id="as1" class="entity.Aspect1"></bean>
<aop:config>
	<aop:aspect  ref="as1"><!--  設置切面類 ref表示切面的id -->
	    <aop:pointcut expression="execution(* service.*.*(..))" id="dd"/>    <!--  設置執行某個執行點時運行切面    service.*.*(..)表示service包中的所有類的所有方法執行的時候都出發這個切面執行 -->
	    <aop:after method="after"  pointcut-ref="dd"/> <!--  執行點之后運行切面類中的after方法 -->
	    <aop:before method="before"  pointcut-ref="dd"/>	 <!--  執行點之前運行切面類中的before方法 -->
	</aop:aspect>
</aop:config>

</beans>

②Junit運行TestDemo中的test方法得到結果

二:基於注解的配置##

①applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
		
<bean id="s1" class="service.Service1"></bean>
<bean id="as1" class="entity.Aspect1"></bean>
<aop:aspectj-autoproxy  proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

②:Aspect.java的代碼,其他兩個類不變

package entity;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
public class Aspect1 {
		
		@Before("execution(* service.*.*(..))")
		public void before(){
			System.out.println("業務執行前,before方法被執行");
		}
		@After("execution(* service.*.*(..))")
		public void after(){
			System.out.println("業務執行后,after方法被執行");
		}
}

③:Junit運行TestDemo中的test方法得到結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM