Spring AOP之Hello World
2004-10-21 09:57
316 查看
我们使用一个简单的例子来演示一下Spring中的AOP,这是一个log的例子,实际上log是一个对于AOP来说很不好的例子,这里我们只为说明Spring AOP的使用。
一,首先我们来创建一个自己的interceptor。
这个类必须继承 org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是参照 aopalliance这个标准实现的,所以我们的MyInterceptor要继承这个标准中的接口。
这个接口只有一个要求实现的方法:
public Object invoke(MethodInvocation methodInvocation) throws Throwable;
下面是我们的MyIntercptor:
public class MyInterceptor implements MethodInterceptor {
private final Log logger = LogFactory.getLog(getClass());
public Object invoke(MethodInvocation methodInvocation) throws Throwable
{
logger.info("Beginning method (1): " + methodInvocation.getMethod().getDeclaringClass() + "." + methodInvocation.getMethod().getName() + "()");
long startTime = System.currentTimeMillis();
try
{
Object result = methodInvocation.proceed();
return result;
}
finally{
logger.info("Ending method (1): " + methodInvocation.getMethod().getDeclaringClass() + "." + methodInvocation.getMethod().getName() + "()"); l
ogger.info("Method invocation time (1): " + (System.currentTimeMillis() - startTime) + " ms.");
}
}
}
对于上面的代码需要说明的是下面两行代码:
Object result = methodInvocation.proceed(); return result; 整个程序的流程是这样的:
1,先是执行在Object result = methodInvocation.proceed();前面的代码;
2,接着执行Object result = methodInvocation.proceed();,它把执行控制权交给了interceptor stack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;
3,然后执行return result;之前的代码;
4,最后执行return result;,它把控制权交回它之上的interceptor,如果没有了就退出interceptor stack。
二,写出我们的业务对象及其接口为了方便我们的业务接口只有一个hello方法:
public interface BusinessInterface
{
public void hello();
}
业务对象的代码如下:
public class BusinessInterfaceImpl implements BusinessInterface
{
public void hello()
{
System.out.println("hello Spring AOP.");
}
}
三,接下来,我们来看看如何使用我们的写的interceptor。我们把业务对象作为AOP的target:
接着在bean定义中声明interceptor:
最后,我们来声明真正的业务对象,通过使用它的接口以及Spring的ProxyFactoryBean: com.rst.spring.testaop.BusinessInterfacemyInterceptorbusinessTarget
这里需要说明两点:
proxyInterfaces:就是我们的业务对象的实际接口;
interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。
四,最后,写我们的测试类
ClassPathResource resource = new ClassPathResource("com/rst/spring/testaop/aop_bean.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
BusinessInterface businessBean = (BusinessInterface) beanFactory.getBean("businessBean");
businessBean.hello();
一切正常就可以在log上看到相应的信息了。
以下是附件源代码的执行效果:
2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello() hello Spring AOP.
2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello()
2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms. 源代码需要spring.jar, aopallience.jar, commons-logging.jar。
再来介绍一下JDK中的动态代理:Dynamic Proxy。
Spring AOP就是利用动态代理实现,包括另一个有名的AOP框架Nanning。
首先,是我们的接口以及实现类:
接着是proxy类:
public class SpeakProxy implements InvocationHandler {
private Logger logger = Logger.getLogger(this.getClass());
private Object callee;
private SpeakProxy(Object callee){
this.callee = callee;
}
public static Speakable getProxy(Object callee){
return (Speakable) Proxy.newProxyInstance(callee.getClass().getClassLoader(),
callee.getClass().getInterfaces(),
new SpeakProxy(callee));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
Object result = null;
logger.info("before method invoke. " + method.getName() + "()");
try {
result = method.invoke(callee, args);
} catch (IllegalArgumentException e) {
logger.info(method.getName() + " has illegal argument exception.");
e.printStackTrace();
} catch (IllegalAccessException e) {
logger.info(method.getName() + " has illegal access exception.");
e.printStackTrace();
} catch (InvocationTargetException e) {
logger.info(method.getName() + " has invocation target exception.");
e.printStackTrace();
}finally{
logger.info("after mthod invoke. " + method.getName() + "()");
}
return result;
}
}
注意在newProxy返回的时候,你可以只返回Object,然后再调用的时候把它cast需要的接口。
这里为了简单和清楚,只返回Speakable。
最后看如何使用这个proxy:
public class Test {
public static void main(String[] args) {
Speakable speak = SpeakProxy.getProxy(new SpeakHello());
speak.speak();
}
}
如果一切正常你就可以看到如下的信息:
2004-09-09 10:35:16,655 INFO - before method invoke. speak()
2004-09-09 10:35:16,665 INFO - hello
2004-09-09 10:35:16,665 INFO - after mthod invoke. speak()
(程序中使用了log4j.jar,可以直接改为System.out.println更简单些)
ezerg 编程小语
一,首先我们来创建一个自己的interceptor。
这个类必须继承 org.aopalliance.intercept. MethodInterceptor接口。Spring的AOP框架就是参照 aopalliance这个标准实现的,所以我们的MyInterceptor要继承这个标准中的接口。
这个接口只有一个要求实现的方法:
public Object invoke(MethodInvocation methodInvocation) throws Throwable;
下面是我们的MyIntercptor:
public class MyInterceptor implements MethodInterceptor {
private final Log logger = LogFactory.getLog(getClass());
public Object invoke(MethodInvocation methodInvocation) throws Throwable
{
logger.info("Beginning method (1): " + methodInvocation.getMethod().getDeclaringClass() + "." + methodInvocation.getMethod().getName() + "()");
long startTime = System.currentTimeMillis();
try
{
Object result = methodInvocation.proceed();
return result;
}
finally{
logger.info("Ending method (1): " + methodInvocation.getMethod().getDeclaringClass() + "." + methodInvocation.getMethod().getName() + "()"); l
ogger.info("Method invocation time (1): " + (System.currentTimeMillis() - startTime) + " ms.");
}
}
}
对于上面的代码需要说明的是下面两行代码:
Object result = methodInvocation.proceed(); return result; 整个程序的流程是这样的:
1,先是执行在Object result = methodInvocation.proceed();前面的代码;
2,接着执行Object result = methodInvocation.proceed();,它把执行控制权交给了interceptor stack(拦截器栈)内的下一个interceptor,如果没有了就交给真正的业务方法;
3,然后执行return result;之前的代码;
4,最后执行return result;,它把控制权交回它之上的interceptor,如果没有了就退出interceptor stack。
二,写出我们的业务对象及其接口为了方便我们的业务接口只有一个hello方法:
public interface BusinessInterface
{
public void hello();
}
业务对象的代码如下:
public class BusinessInterfaceImpl implements BusinessInterface
{
public void hello()
{
System.out.println("hello Spring AOP.");
}
}
三,接下来,我们来看看如何使用我们的写的interceptor。我们把业务对象作为AOP的target:
接着在bean定义中声明interceptor:
最后,我们来声明真正的业务对象,通过使用它的接口以及Spring的ProxyFactoryBean: com.rst.spring.testaop.BusinessInterfacemyInterceptorbusinessTarget
这里需要说明两点:
proxyInterfaces:就是我们的业务对象的实际接口;
interceptorNames:定义了所有interceptors的执行顺序,其中业务对象的target作为list的最后一个。记着一定要把业务对象的target放到list中,否则你的业务对象就不会工作。
四,最后,写我们的测试类
ClassPathResource resource = new ClassPathResource("com/rst/spring/testaop/aop_bean.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(resource);
BusinessInterface businessBean = (BusinessInterface) beanFactory.getBean("businessBean");
businessBean.hello();
一切正常就可以在log上看到相应的信息了。
以下是附件源代码的执行效果:
2004-09-08 16:04:51,210 INFO - Beginning method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
2004-09-08 16:04:51,210 INFO - Beginning method (2): interface com.rst.spring.testaop.BusinessInterface.hello() hello Spring AOP.
2004-09-08 16:04:51,210 INFO - Ending method (2): interface com.rst.spring.testaop.BusinessInterface.hello()
2004-09-08 16:04:51,210 INFO - Ending method (1): interface com.rst.spring.testaop.BusinessInterface.hello()
2004-09-08 16:04:51,210 INFO - Method invocation time (1): 0 ms. 源代码需要spring.jar, aopallience.jar, commons-logging.jar。
再来介绍一下JDK中的动态代理:Dynamic Proxy。
Spring AOP就是利用动态代理实现,包括另一个有名的AOP框架Nanning。
首先,是我们的接口以及实现类:
public interface Speakable { public void speak(); } public class SpeakHello implements Speakable{ private Logger logger = Logger.getLogger(getClass()); public void speak() { logger.info("hello"); } } |
public class SpeakProxy implements InvocationHandler {
private Logger logger = Logger.getLogger(this.getClass());
private Object callee;
private SpeakProxy(Object callee){
this.callee = callee;
}
public static Speakable getProxy(Object callee){
return (Speakable) Proxy.newProxyInstance(callee.getClass().getClassLoader(),
callee.getClass().getInterfaces(),
new SpeakProxy(callee));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
Object result = null;
logger.info("before method invoke. " + method.getName() + "()");
try {
result = method.invoke(callee, args);
} catch (IllegalArgumentException e) {
logger.info(method.getName() + " has illegal argument exception.");
e.printStackTrace();
} catch (IllegalAccessException e) {
logger.info(method.getName() + " has illegal access exception.");
e.printStackTrace();
} catch (InvocationTargetException e) {
logger.info(method.getName() + " has invocation target exception.");
e.printStackTrace();
}finally{
logger.info("after mthod invoke. " + method.getName() + "()");
}
return result;
}
}
注意在newProxy返回的时候,你可以只返回Object,然后再调用的时候把它cast需要的接口。
这里为了简单和清楚,只返回Speakable。
最后看如何使用这个proxy:
public class Test {
public static void main(String[] args) {
Speakable speak = SpeakProxy.getProxy(new SpeakHello());
speak.speak();
}
}
如果一切正常你就可以看到如下的信息:
2004-09-09 10:35:16,655 INFO - before method invoke. speak()
2004-09-09 10:35:16,665 INFO - hello
2004-09-09 10:35:16,665 INFO - after mthod invoke. speak()
(程序中使用了log4j.jar,可以直接改为System.out.println更简单些)
ezerg 编程小语
相关文章推荐
- Spring.AOP下的 hello world !
- Spring AOP之Hello World
- Spring AOP之Hello World
- spring & aop- hello world (一)
- Spring AOP 的 Hello World
- Spring AOP之Hello World
- Spring的AOP编程
- 框架学习之Spring 第三节 采用Spring实现AOP功能
- Spring aop面向切面编程概述
- [置顶] spring切点切面aop——xml非注解
- springboot 自定义注解+AOP 实现日志记录
- 彻底征服 Spring AOP 之 理论篇
- Spring学习总结(三)——Spring实现AOP的多种方式
- redis与spring aop整合
- Spring Boot中使用AOP统一处理Web请求日志
- spring中IOC,AOP
- Spring中的面向切面编程(AOP)简介
- Spring从菜鸟到高手(二)AOP的真正实现
- 自己动手实现最简单版本的Spring AOP
- Spring AOP编程-aspectJ通知类型-6大通知