Spring AOP用到的切面,通知例子

來源:互聯網
上載者:User

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
歡迎下次光臨!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.