Spring AOP用到的切面,通知例子
2012-06-06 08:27
363 查看
1.接口类 ComputerInterf.java
2. 类 Computer.java
3. 代理类 DaiLi1.java
4.实现类 MainAop.java
5.配置文件 applicationContext.xml
.测试结果
log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
欢迎光临
电脑名称为:HPT7600;价格为:5000
欢迎下次光临!
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
欢迎下次光临!
相关文章推荐
- Spring AOP-通知-为目标方法织入多个切面
- Spring容器切面编程 aop注解开发 五中通知类型
- Spring4笔记----AOP的环绕通知、切面的优先级以及重用切入点定义
- spring03 AOP、各种通知、一/多个切面例子
- Spring Aop中四个重要概念,切点,切面,连接点,通知
- Spring面向切面(AOP)的例子
- 0047 Spring的AOP入门基础--切面组件--通知--切入点
- Spring AOP 面向切面编程 常见通知实现(前置,后置,环绕,异常)
- Spring 4.0 学习日记(8) ---AOP切面注解实现五种通知
- Spring AOP高级——源码实现(2)Spring AOP中通知器(Advisor)与切面(Aspect)
- Spring--AOP--基本配置(切面Aspect,切入点Pointcut,通知Advice)
- spring aop事务通知(切面异常处理)
- Spring中基于aop命名空间的AOP 二(声明一个切面、切入点和通知)
- Spring学习(九)-AOP切面通知
- Spring学习笔记3--面向切面(AOP)的例子
- 【spring】aop切面通知,日志处理
- 5、AOP例子(切面,通知,切入点)
- Spring-AOP的五种通知和切面的优先级、通知变量声明
- Spring AOP面向切面编程一个简单例子和在配置过程中出现错误
- Spring AOP 切面与通知【Spring 入门】