打造第一个Spring AOP的示例
来源:互联网 发布:淘宝卖家买家秀怎么弄 编辑:程序博客网 时间:2024/04/30 11:15
俺是用Eclipse作的,看过之后做做看,体验一下AOP是什么东东阿!好,不多废话了,让我们开始吧.
第一步 建立一个接口
package com.neusoft.impl;
public interface IBusinessLogic {
public void foo () ;
}
public interface IBusinessLogic {
public void foo () ;
}
第二步 建立接口的实现类
package com.neusoft.action;
import com.neusoft.impl.IBusinessLogic;
public class BusinessLogic implements IBusinessLogic {
public void foo() {
System.out.println("Inside BusinessLogic.foo()");
}
}
import com.neusoft.impl.IBusinessLogic;
public class BusinessLogic implements IBusinessLogic {
public void foo() {
System.out.println("Inside BusinessLogic.foo()");
}
}
第三步 建立Before通知
package com.neusoft.action;
import java.lang.reflect.Method ;
import org.springframework.aop.MethodBeforeAdvice ;
public class TracingBeforeAdvice implements MethodBeforeAdvice {
public void before ( Method m, Object[] args, Object target ) throws Throwable {
System.out.println("Hello lisi!(by" + this.getClass().getName() + ")") ;
}
}
import java.lang.reflect.Method ;
import org.springframework.aop.MethodBeforeAdvice ;
public class TracingBeforeAdvice implements MethodBeforeAdvice {
public void before ( Method m, Object[] args, Object target ) throws Throwable {
System.out.println("Hello lisi!(by" + this.getClass().getName() + ")") ;
}
}
第四步 建立After通知
package com.neusoft.action;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice ;
public class TracingAfterAdvice implements AfterReturningAdvice {
public void afterReturning ( Object object, Method m, Object[] args, Object target )
throws Throwable {
System.out.println ("Hello lisi!(by" + this.getClass().getName() + ")") ;
}
}
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice ;
public class TracingAfterAdvice implements AfterReturningAdvice {
public void afterReturning ( Object object, Method m, Object[] args, Object target )
throws Throwable {
System.out.println ("Hello lisi!(by" + this.getClass().getName() + ")") ;
}
}
第五步 建立配置文件(springconfig.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Bean configuration -->
<bean id = "businesslogicbean"
class = "org.springframework.aop.framework.ProxyFactoryBean">
<property name = "proxyInterfaces">
<value>com.neusoft.impl.IBusinessLogic</value>
</property>
<property name="target">
<ref local = "beanTarget"/>
</property>
</bean>
<!-- Bean Classes -->
<bean id = "beanTarget"
class = "com.neusoft.action.BusinessLogic">
</bean>
<!-- Advice classes -->
<bean id = "theTracingBeforeAdvice"
class = "com.neusoft.action.TracingBeforeAdvice">
</bean>
<bean id = "theTracingAfterAdvice"
class = "com.neusoft.action.TracingAfterAdvice">
</bean>
<!-- Advisor pointcut definition for before advice -->
<bean id = "theTracingBeforeAdvisor"
class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name = "advice">
<ref local = "theTracingBeforeAdvice"/>
</property>
<property name="patterns">
<value>.*</value>
</property>
</bean>
<!-- Advisor pointcut definition for after advice -->
<bean id = "theTracingAfterAdvisor"
class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name = "advice">
<ref local = "theTracingAfterAdvice"/>
</property>
<property name = "patterns">
<value>.*</value>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Bean configuration -->
<bean id = "businesslogicbean"
class = "org.springframework.aop.framework.ProxyFactoryBean">
<property name = "proxyInterfaces">
<value>com.neusoft.impl.IBusinessLogic</value>
</property>
<property name="target">
<ref local = "beanTarget"/>
</property>
</bean>
<!-- Bean Classes -->
<bean id = "beanTarget"
class = "com.neusoft.action.BusinessLogic">
</bean>
<!-- Advice classes -->
<bean id = "theTracingBeforeAdvice"
class = "com.neusoft.action.TracingBeforeAdvice">
</bean>
<bean id = "theTracingAfterAdvice"
class = "com.neusoft.action.TracingAfterAdvice">
</bean>
<!-- Advisor pointcut definition for before advice -->
<bean id = "theTracingBeforeAdvisor"
class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name = "advice">
<ref local = "theTracingBeforeAdvice"/>
</property>
<property name="patterns">
<value>.*</value>
</property>
</bean>
<!-- Advisor pointcut definition for after advice -->
<bean id = "theTracingAfterAdvisor"
class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name = "advice">
<ref local = "theTracingAfterAdvice"/>
</property>
<property name = "patterns">
<value>.*</value>
</property>
</bean>
</beans>
第六步 建立测试类
package com.neusoft.test;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.neusoft.impl.IBusinessLogic;
public class MainApplication {
/**
* @author lishijin
*/
public static void main(String[] args) {
//Read the configration file
ApplicationContext ctx = new FileSystemXmlApplicationContext("/WEB-INF/springconfig.xml") ;
//Instantiate an object
IBusinessLogic testObject = (IBusinessLogic)ctx.getBean("businesslogicbean");
//Execute the public
//method of the bean
testObject.foo() ;
}
}
import org.springframework.context.ApplicationContext ;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.neusoft.impl.IBusinessLogic;
public class MainApplication {
/**
* @author lishijin
*/
public static void main(String[] args) {
//Read the configration file
ApplicationContext ctx = new FileSystemXmlApplicationContext("/WEB-INF/springconfig.xml") ;
//Instantiate an object
IBusinessLogic testObject = (IBusinessLogic)ctx.getBean("businesslogicbean");
//Execute the public
//method of the bean
testObject.foo() ;
}
}
运行结果:
其中我没有做出详细的解释,我想你动手做之后,自然会知道是怎么回事,最近在研究AOP技术,敬请关注下我的博客
- 打造第一个Spring AOP的示例
- Spring的AOP配置 第一个AOP例子
- 第一个Spring AOP实例
- 第一个Spring AOP实例
- spring aop 第一个程序
- 第一个spring mvc示例
- 第一个spring boot示例
- Spring学习之第一个AOP程序
- spring的aop示例代码
- SPRING AOP 定义的示例
- Spring的aop简单示例
- Stand alone的Spring示例 (AOP)
- spring的AOP模块配置示例
- Spring AOP的理解与配置示例
- spring aop学习的第一个例子
- spring Aop简单示例
- Spring aop简单示例
- spring aop简单示例
- 内核之旅-概述篇 上
- 宾馆管理系统开题报告范文
- 开题报告的撰写方法
- 一个开始:char* 和 char[]的区别
- 手机LINUX系统移植的想法
- 打造第一个Spring AOP的示例
- Ajax原理详细说明(转自ibm开发者网站)
- Linux HAL (Hardware Abstraction Layer)的工作原理
- Flash文件调用txt文件是出现乱码时的解决办法
- [转]网络安全工具开发函数库介绍
- 安装Visual .net软件版本错误绝招
- 汇编16位部分学习总结!
- J2ee核心技术:J2EE Frameworks介绍
- 做个有钱人之借鉴篇:成为富人是一种选择