闲人野居
好好学习,天天向上
posts - 57,  comments - 137,  trackbacks - 0

前面这是讲了许多的概念,下面以一个例子来说明:
一个很好理解的aop 例子,也就是日志服务。
先从aop第一种方式来实现,也就是xml配置方式
先创建基本的日志类:
public class Logger {

    private static Log log = LogFactory.getLog(Logger.class);

    public void entry(String message) {
        log.info(message);
    }
}
这里只是简单的一个方法,当然实际情况可能不同。
由于xml配置需要一个方面的实现bean
所以创建一个简单的bean :
public class LogBean {

    private Logger logger = new Logger();

    public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.entry("before invoke method:"
                     + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        logger.entry("after invoke method:"
                     + joinPoint.getSignature().getName());
        return object;
    }
}
这里采取简单的around advice,其他类型的advice 基本上都差不多

当然有了这两个核心的日志类,需要一个测试类,用于测试。
public class TestBean {

    public void method1() {
        System.out.println("in method1");
    }

    public void method2() {
        System.out.println("in method2");
    }
}
这就是需要测试的类了,需要记录日志的方法只有两个,这里用System.out.println,只是想显示方法的调用顺序。


然后关键的在于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"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
 
 <aop:config>
    <!--  expression 表示要执行的匹配表达式,这里匹配所有的public方法,但是去除logger类的所有方法,防止无限调用-->

      <aop:pointcut id="loggableCalls"
          expression="execution(public * *(..)) and !execution(* org.spring.test.aop.log.Logger.*(..))"/>


  <aop:aspect id="logAspect" ref="logBean">
   <aop:around pointcut-ref="loggableCalls" method="aroundLogCalls"/>
  </aop:aspect>
 
 </aop:config>
 <bean id="logBean" class="org.spring.test.aop.log.LogBean" />
 <bean id="testBean" class="org.spring.test.aop.log.TestBean"/>
 
</beans>

现在写一个测试类:

public class LogXmlTest extends RootTest {

    @Override
    protected String getBeanXml() {
        return "org/spring/test/aop/log/bean.xml";
    }

    public void testLog() {
        TestBean bean = (TestBean) ctx.getBean("testBean");
        bean.method1();
        bean.method2();
    }

}

public abstract class RootTest extends TestCase {

    protected ApplicationContext  ctx;

    protected Log log = LogFactory.getLog(getClass());

    protected RootTest() {
        ctx = new ClassPathXmlApplicationContext(getBeanXml());
    }

    protected abstract String getBeanXml();

}

打印的消息如下:
2006-09-17 11:08:28,203 INFO [org.spring.test.aop.log.Logger] - before invoke method:method1
in method1
2006-09-17 11:08:28,203 INFO [org.spring.test.aop.log.Logger] - after invoke method:method1
2006-09-17 11:08:28,218 INFO [org.spring.test.aop.log.Logger] - before invoke method:method2
in method2
2006-09-17 11:08:28,218 INFO [org.spring.test.aop.log.Logger] - after invoke method:method2

第二种实现方式,采用注释方式:

Logger 类不变
创建一个LogAspect类
@Aspect
public class LogAspect {

    private Logger logger = new Logger();

    @Pointcut("execution(public * *(..))")
    public void publicMethods() {

    }

    @Pointcut("execution(* org.spring.test.aop.log.Logger.*(..))")
    public void logObjectCalls() {

    }

    @Pointcut("publicMethods()&&!logObjectCalls()")
    public void loggableCalls() {

    }

    @Around("loggableCalls()")
    public Object aroundLogCalls(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.entry("before invoke method:"
                     + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        logger.entry("after invoke method:"
                     + joinPoint.getSignature().getName());
        return object;
    }
}

配置文件就简单多了
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
 
 <aop:aspectj-autoproxy/>
 
 <!-- 或者使用以下定义
 
 
 
 <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
 
 -->
 <bean id="logAspect" class="org.spring.test.aop.log.LogAspect"/>
 <bean id="testBean" class="org.spring.test.aop.log.TestBean"/>
 
</beans>

测试类:
跟上面的差不多
把xml文件换掉就行

打印的方式差不多

个人还是比较喜欢第二种实现。

posted on 2006-09-17 11:13 布衣郎 阅读(2797) 评论(4)  编辑  收藏 所属分类: aopspring

FeedBack:
# re: spring aop 2.0 编程(三)
2006-09-26 18:23 | 旱头憨脑
hi,我完全按照你的配置来写的,为什么不能跑起来呢?
请问有什么需要注意的地方么?  回复  更多评论
  
# re: spring aop 2.0 编程(三)
2006-10-20 16:54 | 捕风
可不可以通过这种方式在日志中输出bo的业务数据(testBean.toString()).
pointcut:myapp.service.bo.*
  回复  更多评论
  
# re: spring aop 2.0 编程(三)
2006-10-21 08:33 | 布衣郎
@捕风
应该可以,只要定义参数传递就行,通过this 或者target来传递
  回复  更多评论
  
# re: spring aop 2.0 编程(三)
2006-11-11 21:43 | 冰川
这个好啊~~~
支持下。。。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(12)

随笔分类(59)

随笔档案(57)

blog

java

uml

搜索

  •  

积分与排名

  • 积分 - 355562
  • 排名 - 154

最新评论

阅读排行榜

评论排行榜