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

          前面這是講了許多的概念,下面以一個(gè)例子來(lái)說(shuō)明:
          一個(gè)很好理解的aop 例子,也就是日志服務(wù)。
          先從aop第一種方式來(lái)實(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);
          ??? }
          }
          這里只是簡(jiǎn)單的一個(gè)方法,當(dāng)然實(shí)際情況可能不同。
          由于xml配置需要一個(gè)方面的實(shí)現(xiàn)bean
          所以創(chuàng)建一個(gè)簡(jiǎn)單的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;
          ??? }
          }
          這里采取簡(jiǎn)單的around advice,其他類型的advice 基本上都差不多

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

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

          ??? public void method2() {
          ??????? System.out.println("in method2");
          ??? }
          }
          這就是需要測(cè)試的類了,需要記錄日志的方法只有兩個(gè),這里用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類的所有方法,防止無(wú)限調(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)在寫一個(gè)測(cè)試類:

          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)建一個(gè)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;
          ??? }
          }

          配置文件就簡(jiǎn)單多了
          <?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>

          測(cè)試類:
          跟上面的差不多
          把xml文件換掉就行

          打印的方式差不多

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


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

          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357339
          • 排名 - 155

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 德庆县| 神农架林区| 迁西县| 红原县| 黎平县| 斗六市| 榕江县| 高陵县| 白沙| 衡阳市| 永安市| 神池县| 阿瓦提县| 鹤山市| 浮梁县| 耿马| 合阳县| 石狮市| 聂拉木县| 会昌县| 康平县| 乃东县| 台南市| 勃利县| 神农架林区| 衡阳县| 永州市| 名山县| 潞西市| 改则县| 蓝田县| 八宿县| 随州市| 潮州市| 宜黄县| 无为县| 巴林左旗| 赤城县| 和田市| 红安县| 临沂市|