您的位置:首页 > 编程语言 > Java开发

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

2012-06-06 08:27 363 查看
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

欢迎下次光临!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: