閑人野居
          好好學(xué)習(xí),天天向上
          posts - 57,  comments - 137,  trackbacks - 0

          前面這是講了許多的概念,下面以一個例子來說明:
          一個很好理解的aop 例子,也就是日志服務(wù)。
          先從aop第一種方式來實(shí)現(xiàn),也就是xml配置方式
          先創(chuàng)建基本的日志類:
          public class Logger {

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

          ??? public void entry(String message) {
          ??????? log.info(message);
          ??? }
          }
          這里只是簡單的一個方法,當(dāng)然實(shí)際情況可能不同。
          由于xml配置需要一個方面的實(shí)現(xiàn)bean
          所以創(chuàng)建一個簡單的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 基本上都差不多

          當(dāng)然有了這兩個核心的日志類,需要一個測試類,用于測試。
          public class TestBean {

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

          ??? public void method2() {
          ??????? System.out.println("in method2");
          ??? }
          }
          這就是需要測試的類了,需要記錄日志的方法只有兩個,這里用System.out.println,只是想顯示方法的調(diào)用順序。


          然后關(guān)鍵的在于xml的配置了

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="?xmlns:xsi="?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 表示要執(zhí)行的匹配表達(dá)式,這里匹配所有的public方法,但是去除logger類的所有方法,防止無限調(diào)用-->

          ??????<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>

          現(xiàn)在寫一個測試類:

          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

          第二種實(shí)現(xiàn)方式,采用注釋方式:

          Logger 類不變
          創(chuàng)建一個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="?xmlns:xsi="?xmlns:aop="?xsi:schemaLocation="
          http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop?
          ?<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文件換掉就行

          打印的方式差不多

          個人還是比較喜歡第二種實(shí)現(xiàn)。


          FeedBack:
          # re: spring aop 2.0 編程(三)
          2006-09-26 18:23 | 旱頭憨腦
          hi,我完全按照你的配置來寫的,為什么不能跑起來呢?
          請問有什么需要注意的地方么?  回復(fù)  更多評論
            
          # re: spring aop 2.0 編程(三)
          2006-10-20 16:54 | 捕風(fēng)
          可不可以通過這種方式在日志中輸出bo的業(yè)務(wù)數(shù)據(jù)(testBean.toString()).
          pointcut:myapp.service.bo.*
            回復(fù)  更多評論
            
          # re: spring aop 2.0 編程(三)
          2006-10-21 08:33 | 布衣郎
          @捕風(fēng)
          應(yīng)該可以,只要定義參數(shù)傳遞就行,通過this 或者target來傳遞
            回復(fù)  更多評論
            
          # re: spring aop 2.0 編程(三)
          2006-11-11 21:43 | 冰川
          這個好啊~~~
          支持下。。。  回復(fù)  更多評論
            

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

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357739
          • 排名 - 155

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 香河县| 长丰县| 周宁县| 格尔木市| 苏尼特左旗| 东源县| 林州市| 乌什县| 河南省| 孙吴县| 宁津县| 工布江达县| 凭祥市| 山西省| 怀远县| 邹平县| 玉林市| 治县。| 祁东县| 永和县| 南投县| 都匀市| 齐河县| 泰来县| 安新县| 贵阳市| 双辽市| 邯郸县| 大渡口区| 杨浦区| 吴堡县| 杭州市| 万源市| 卢湾区| 南宁市| 盐池县| 元江| 河津市| 鲁山县| 丹江口市| 德江县|