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

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
欢迎下次光临!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值