Spring 方法拦截示例

  1. /**
  2.  * 
  3.  */
  4. package com;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. /**
  8.  * @author Administrator write Main.java at : 2007-6-6 涓婂崍08:53:23
  9.  * 
  10.  */
  11. public class Main {
  12.     public final static String name = "bean";
  13.     /**
  14.      * @param args
  15.      */
  16.     public static void main(String[] args) {
  17.     //  System.out.println(System.getProperties().toString());
  18.         ApplicationContext ctx = new ClassPathXmlApplicationContext(
  19.                 "**/applicationContext.xml");
  20.         
  21.         
  22. /*
  23.         Resource r = ctx.getResource("file:c:/aa.txt");
  24.         String ss = "${user.dir}aaa";
  25.         System.out.println(SystemPropertyUtils.resolvePlaceholders(ss));
  26.         System.out.println(System.getProperties().toString());
  27.         
  28.         */
  29.         
  30.     
  31.         Bean7 b = (Bean7) ctx.getBean("b7");
  32.         
  33.         b.transfer();
  34.         // System.out.println(r.exists());
  35.         /*
  36.          * Mymethod b = (Mymethod) ctx.getBean("beanTarget");
  37.          * 
  38.          * b.listPersons();
  39.          */
  40.         /*
  41.          * for (int i = 1; i < 10; i++) { b.addPerson(i+"", i+""); }
  42.          */
  43.         /*
  44.          * Bean7 b=(Bean7) ctx.getBean("b7");
  45.          * 
  46.          * 
  47.          * 
  48.          * b.transfer();
  49.          */
  50.         
  51.         
  52.         /*String s = (String) ctx.getBean("myfactoryBean");
  53.          Mymethod b = (Mymethod) ctx.getBean(name);
  54.          
  55.         
  56.          b.addPerson("sss", "sss");
  57.          b.listPersons();*/
  58.         
  59.         
  60.         
  61.         
  62.         
  63.         /*
  64.          * 
  65.          * 
  66.          * Resource resource=ctx.getResource("file:jdbc.properties");
  67.          * 
  68.          * System.out.println( resource.getFilename());
  69.          * 
  70.          * 
  71.          * 
  72.          * try { BufferedInputStream bis=new
  73.          * BufferedInputStream(resource.getInputStream()); byte[] s=new
  74.          * byte[bis.available()];
  75.          * 
  76.          * 
  77.          * bis.read(s);
  78.          * 
  79.          * 
  80.          * System.out.println(new String(s)) ; } catch (IOException e) { // TODO
  81.          * Auto-generated catch block e.printStackTrace(); }
  82.          */
  83.         /*
  84.          * String[] s=new String[2];
  85.          * 
  86.          * s[0]="ssss"; s[1]="00000";
  87.          * System.out.println(ctx.getMessage("message", s,"ss",null) );
  88.          * 
  89.          * NewBean nb=(NewBean) ctx.getBean("NewBean"); System.out.println(
  90.          * nb.getB().getName());
  91.          */
  92.         /*
  93.          * CommandManager commandManager=(CommandManager)
  94.          * ctx.getBean("commandManager");
  95.          * 
  96.          * 
  97.          * commandManager.process();
  98.          */
  99.     }
  100. }

 

 

 

 

 

 



    

 

  1. /**
  2.  * 
  3.  */
  4. package com;
  5. import org.hibernate.SessionFactory;
  6. import org.springframework.orm.hibernate3.HibernateTemplate;
  7. import person.PersonModel;
  8. /**
  9.  * @author Administrator write Bean.java at : 2007-6-6 涓婂崍08:56:16
  10.  * 
  11.  */
  12. public class Bean7 {
  13.     
  14.     
  15.     public void transfer()
  16.     {
  17.         
  18.         
  19.         System.out.println("transfering");
  20.         
  21.     }
  22.     
  23. }

 

 

 

 

 

  1. package org.xyz;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class NotVeryUsefulAspect   {
  4.     
  5.     
  6.     public void before(){
  7.         
  8.         
  9.         
  10.         System.out.println("before   ");
  11.         
  12.         
  13.     }
  14.     
  15.     public void after_returning(){
  16.         
  17.         System.out.println("after_returning");
  18.         
  19.         
  20.     }
  21.     public void after_throwing(){
  22.         
  23.         System.out.println("after_throwing");
  24.         
  25.         
  26.     }
  27.     public void around(ProceedingJoinPoint pjp) throws Throwable{
  28.         System.out.println("around");
  29.         pjp.proceed();
  30.         
  31.         
  32.     
  33.         
  34.         
  35.     }
  36.     public int getOrder() {
  37.         // TODO Auto-generated method stub
  38.         return 1000;
  39.     }
  40.     
  41. }

 

 

 

 

 

 

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.         xmlns:context="http://www.springframework.org/schema/context"
  4.         xmlns:tx="http://www.springframework.org/schema/tx"
  5.         xmlns:aop="http://www.springframework.org/schema/aop"
  6.     
  7.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8.                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  9.                 
  10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  11.                 http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12.                 ">
  13. <import   resource="hibernate.xml"/>
  14. <import   resource="aop_schema.xml"/>
  15. <!--
  16. <import   resource="oa.xml"/>
  17. <import   resource="inludedContext.xml"/>
  18. --></beans>

 

 

 

 

 

 

 

 

 

 

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:tx="http://www.springframework.org/schema/tx"
  6.     xmlns:aop="http://www.springframework.org/schema/aop"
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8.                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  9.                 
  10.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  11.                 http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12.                 ">
  13.     <bean id="b7" class="com.Bean7">
  14.     </bean>
  15.     <bean id="myAspect" class="org.xyz.NotVeryUsefulAspect"></bean>
  16.     <bean id="myAspect2" class="org.xyz.NotVeryUsefulAspect2"></bean>
  17.     <aop:config>
  18.         <aop:pointcut id="businessService"
  19.             expression="execution(public * com.Bean7.*(..))" />
  20.         <aop:aspect id="TestAspect" ref="myAspect">
  21.         
  22.                 
  23.                 <aop:before pointcut-ref="businessService" method="before" />
  24.                 
  25.                 <aop:after-returning pointcut-ref="businessService"
  26.                 method="after_returning" />
  27.                 <aop:after-throwing pointcut-ref="businessService"
  28.                 method="after_throwing" />
  29.                 
  30.             
  31.             <aop:around pointcut-ref="businessService" method="around" />
  32.         </aop:aspect>
  33.         <aop:aspect id="TestAspect2" ref="myAspect2">
  34.             <!--
  35.                 
  36.                 <aop:before pointcut-ref="businessService" method="before" />
  37.                 
  38.                 <aop:after-returning pointcut-ref="businessService"
  39.                 method="after_returning" />
  40.                 <aop:after-throwing pointcut-ref="businessService"
  41.                 method="after_throwing" />
  42.                 
  43.             -->
  44.             <aop:around pointcut-ref="businessService" method="around" />
  45.         </aop:aspect>
  46.     </aop:config>
  47. </beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值