1.介面類 ComputerInterf.java
package com.stelcomtech;public interface ComputerInterf { public void buyPc(); public void buySome(); public void test();}
2. 類 Computer.java
package com.stelcomtech;public class Computer implements ComputerInterf{ private String pcName="HPT7600"; private int pcPrice=5000; public String getPcName() { return pcName; } public void setPcName(String pcName) { this.pcName = pcName; } public int getPcPrice() { return pcPrice; } public void setPcPrice(int pcPrice) { this.pcPrice = pcPrice; }
package com.stelcomtech;
public class Computer implements ComputerInterf{ private String pcName="HPT7600"; private int pcPrice=5000; public String getPcName() { return pcName; } public void setPcName(String pcName) { this.pcName = pcName; } public int getPcPrice() { return pcPrice; } public void setPcPrice(int pcPrice) { this.pcPrice = pcPrice; } public void buyPc() { System.out.println("電腦名稱為:"+this.getPcName()+";價格為:"+this.getPcPrice()); } public void buySome(){ System.out.println("我只想買顯示器!"); } public void test(){}}
3. 代理類 DaiLi1.java
package com.stelcomtech;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class DaiLi1 implements MethodInterceptor {public Object invoke(MethodInvocation arg0) throws Throwable {System.out.println("本代理送滑鼠");Object object=arg0.proceed();return object;}}
package com.stelcomtech;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class DaiLi implements MethodBeforeAdvice {public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {System.out.println("歡迎光臨");}}
package com.stelcomtech;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class After implements AfterReturningAdvice {public void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {System.out.println("歡迎下次光臨!");}}
4.實作類別 MainAop.java
package com.stelcomtech;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class MainAop {public static void main(String[] args) {ApplicationContext context=new FileSystemXmlApplicationContext("src/com/stelcomtech/applicationContext.xml");ComputerInterf pcInterf=(ComputerInterf)context.getBean("proxy");pcInterf.buyPc();pcInterf.buySome();// pcInterf.test(); 可以測試組態檔案中對整個類的攔截}}
5.設定檔 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="daili" class="com.stelcomtech.DaiLi" abstract="false" lazy-init="default" dependency-check="default"> </bean> <bean id="computer1" class="com.stelcomtech.Computer" abstract="false" lazy-init="default" dependency-check="default"> </bean> <bean id="after" class="com.stelcomtech.After" abstract="false" lazy-init="default" dependency-check="default"> </bean> <!-- 代理Bean --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean" abstract="false" lazy-init="default" dependency-check="default"> <property name="proxyInterfaces"><!-- 代理介面類 --> <value>com.stelcomtech.ComputerInterf</value> </property> <property name="target"><!-- 新的代理 充當 “通知” --> <ref bean="computer1" /> </property> <property name="interceptorNames"><!-- 代理的目標類 --> <list> <value>buybeforeadvisor</value> <value>buyafteradvisor</value> </list> </property> </bean> <!-- buy的通知 --> <bean id="buybeforeadvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" abstract="false" lazy-init="default" dependency-check="default"> <property name="advice"> <ref bean="daili" /> </property> <property name="patterns"><!-- 哪些地方用到代理 --> <value>.*buy.*</value> <!--<value>.*com\.stelcomtech\.ComputerInterf.*</value> --> </property> </bean> <bean id="buyafteradvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" abstract="false" lazy-init="default" dependency-check="default"> <property name="advice"> <ref bean="after" /> </property> <property name="patterns"><!-- 哪些地方用到代理 --> <value>.*buy.*</value> <!--<value>.*com\.stelcomtech\.ComputerInterf.*</value> --> </property> </bean></beans>
.測試結果
log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
歡迎光臨
電腦名稱為:HPT7600;價格為:5000
歡迎下次光臨!