打造第一个Spring AOP的示例
2007-05-03 19:54
495 查看
俺是用Eclipse作的,看过之后做做看,体验一下AOP是什么东东阿!好,不多废话了,让我们开始吧.
第一步 建立一个接口
package com.neusoft.impl;
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()");
}
}
第三步 建立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() + ")") ;
}
}
第四步 建立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() + ")") ;
}
}
第五步 建立配置文件(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>
第六步 建立测试类
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() ;
}
}
运行结果:
其中我没有做出详细的解释,我想你动手做之后,自然会知道是怎么回事,最近在研究AOP技术,敬请关注下我的博客
第一步 建立一个接口
package com.neusoft.impl;
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()");
}
}
第三步 建立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() + ")") ;
}
}
第四步 建立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() + ")") ;
}
}
第五步 建立配置文件(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>
第六步 建立测试类
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() ;
}
}
运行结果:
其中我没有做出详细的解释,我想你动手做之后,自然会知道是怎么回事,最近在研究AOP技术,敬请关注下我的博客
相关文章推荐
- 【Spring-AOP-学习笔记-3】@Before前向增强处理简单示例
- spring AOP 返回后通知 简单示例
- Spring AOP 简单入门示例
- 基于配置的spring AOP的一个示例
- 【我看Spring】从一个简单的AOP示例看切面编程
- Spring AOP示例
- Spring2.0 AOP @AspectJ注解定义切面学习示例。
- Spring AOP(3)基于XML配置实现的示例
- SpringBoot项目创建与第一个示例
- Spring学习之第一个AOP程序
- spring中AOP 注解开发示例详解
- Spring.net架构示例(含Aop和Ioc)源码
- Spring AspectJ AOP 完整示例
- Spring_AOP_简单入门示例
- Spring AOP(面向切面示例)
- 【我看Spring】从一个简单的AOP示例看切面编程
- spring boot aop 记录方法执行时间代码示例
- Spring AOP示例(注解方式)
- spring aop 拦截业务方法,实现权限控制示例
- 【Spring-AOP-学习笔记-4】@After后向增强处理简单示例