1. 程式人生 > >Spring AOP工廠方式的實現(半自動化)

Spring AOP工廠方式的實現(半自動化)

(1)jar包

(2)目錄結構

(3)PersonService.java類
package com.huawei.aop.factory;

public interface PersonService {
	
	public void addPerson();
	public void updatePerson();

}

(4)(3)PersonServiceImpl.java類

package com.huawei.aop.factory;

public class PersonServiceImpl implements PersonService {

	@Override
	public void addPerson() {
		
		System.out.println("factory addperson");
	}

	@Override
	public void updatePerson() {
		
		System.out.println("bfactory addperson");
	}

}

(5)MyAspect.java類
package com.huawei.aop.factory;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MyAspect implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		
		System.out.println("before");
		
		//執行目標方法
		Object obj = mi.proceed();
		
		System.out.println("after");
		return obj;
	}

}

(6)配置檔案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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 1目標類 -->
	<bean id="personServiceId" class="com.huawei.aop.factory.PersonServiceImpl"></bean>
	
	<!-- 2切面類,含有通知 -->
	<bean id="myAspect" class="com.huawei.aop.factory.MyAspect"></bean>
	
	
	<!-- 3代理類 
		* ProxyFactoryBean 用於生產代理類一個特殊的工廠bean
			proxyInterfaces 用於確定需要實現介面
			interceptorNames 用於確定通知實現類,需要提供的bean名稱
			target 用於確定代理類bean名稱
	-->
	<bean id="personServiceProxyId" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!-- 3.1 確定介面 -->
		<property name="proxyInterfaces" value="com.huawei.aop.factory.PersonService"></property>
		<!-- 3.2 確定通知 -->
		<property name="interceptorNames" value="myAspect"></property>
		<!-- 3.3 確定目標類-->
		<property name="target" ref="personServiceId"></property>
		<!-- 3.4 設定true,強制使用 cglib -->
		<property name="optimize" value="true"></property>
	</bean>
</beans>


(7)單元測試類TestApp.java
package com.huawei.aop.factory;

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

public class TestApp {
	@Test
	public void demo01(){
		//獲得目標類
		String xmlPath = "/com/huawei/aop/factory/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		PersonService personService = applicationContext.getBean("personServiceId",PersonService.class);
		personService.addPerson();
		personService.updatePerson();
		
	}
	@Test
	public void demo02(){
		//獲得代理類
		String xmlPath = "/com/huawei/aop/factory/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		PersonService personService = applicationContext.getBean("personServiceProxyId",PersonService.class);
		personService.addPerson();
		personService.updatePerson();
		
	}
}