接前面做的一个例子,我们在注册并操作数据库之前要求输出一句提示语句(仅做示范),即在RegistAction.java中调用方法onSubmit()之前织入一个通知,用来输出提示语句.

如下:
 
定义通知类  StudentDaoBeforeAdvice.java
java 代码
  1. public class StudentDaoBeforeAdvice implements MethodBeforeAdvice {   
  2.     private Student student;   
  3.     public void setStudent(Student student){   
  4.         this.student=student;   
  5.     }   
  6.        
  7.     public void before(Method method, Object[] args, Object target)   
  8.             throws Throwable {   
  9.            
  10.         // TODO Auto-generated method stub   
  11.         //  to do what you want here!    
  12.         System.out.println("通知已织入....数据即将写入");   
  13.        
  14.     }   
  15. }  

下面在Spring配置文件中配置  通知器 ,通知,自动代理
applicationContent.xml

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  3.  "http://www.springframework.org/dtd/spring-beans.dtd">  
  4.   
  5. <beans>  
  6.        
  7.        
  8.     <bean id="viewResolver"    class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  9.         <property name="viewClass">  
  10.         <value>org.springframework.web.servlet.view.JstlViewvalue>  
  11.         </property>  
  12.         <property name="prefix">  
  13.             <value>/result/value>  
  14.         </property>  
  15.         <property name="suffix">  
  16.             <value>.jspvalue>  
  17.         </property>  
  18.    </ bean>  
  19.   
  20.        
  21.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"    
  22. destroy-method="close">  
  23.         <property name="driverClassName">  
  24.             <value>net.sourceforge.jtds.jdbc.Drivervalue>  
  25.         </property>  
  26.         <property name="url">  
  27.             <value>jdbc:jtds:sqlserver://127.0.0.1:1433/testvalue>  
  28.         </property>  
  29.         <property name="username">  
  30.             <value>savalue>  
  31.         property>  
  32.         <property name="password">  
  33.             <value>savalue>  
  34.         </property>  
  35.     </bean>  
  36.   
  37.        
  38.     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  39.         <property name="dataSource">  
  40.             <ref bean="dataSource" />  
  41.         </property>  
  42.     </bean>  
  43.   
  44.        
  45.     <bean id="simpleMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  46.         <property name="urlMap">  
  47.             <map>  
  48.                 <entry key="/regist.do">  
  49.                     <value>RegistActionvalue>  
  50.                 </entry>  
  51.            </ map>  
  52.        </ property>  
  53.    < /bean>  
  54.   
  55.        
  56.   
  57.        
  58.     <bean id="studentDaoTarget" class="phoenix.spring.impl.StudentDaoImp">  
  59.         <property name="jdbcTemplate">  
  60.             <ref bean="jdbcTemplate" />  
  61.         </property>  
  62.     </bean>  
  63.            
  64.              
  65.     <bean id="studentDao" class="org.springframework.aop.framework   
  66. .autoproxy.BeanNameAutoProxyCreator">  
  67.         <property name="beanNames"><value>RegistActionvalue>property>  
  68.         <property name="interceptorNames">  
  69.                 <value>advisorvalue>       
  70.         </property>  
  71.     </bean>  
  72.   
  73.        
  74.     <bean id="advice" class="phoenix.spring.aop.StudentDaoBeforeAdvice">  
  75.     </bean>  
  76.   
  77.        
  78.     <bean id="advisor" class="org.springframework.aop.support.   
  79. RegexpMethodPointcutAdvisor">  
  80.         <property name="advice">  
  81.             <ref local="advice" />  
  82.         </property>  
  83.         <property name="patterns">  
  84.             <list>  
  85.                 <value>.*onSubmit.*value>  
  86.                 org.springframework.web.*   
  87.             </list>  
  88.         property>  
  89.     </bean>  
  90.   
  91.        
  92.     <bean id="RegistAction" class="phoenix.spring.impl.RegistAction">  
  93.         <property name="commandClass">  
  94.             <value>phoenix.spring.model.Studentvalue>  
  95.         </property>  
  96.         <property name="studentDaoImp">  
  97.             <ref local="studentDaoTarget"/>  
  98.        </ property>  
  99.         <property name="success_view">  
  100.             <value>successvalue>  
  101.         </property>  
  102.         <property name="fail_view">  
  103.             <value>failvalue>  
  104.        </ property>  
  105.     </bean>  
  106. </beans>  
*   org.springframework.web.* 在定义切点时,要加上这句代码,否则自动代理无效.详情请见

至此,就可以完成此aop应用了,当注册验证成功并在写入数据之前,会输出信息:
通知已织入....数据即将写入



评论
tiger.passion 2007-04-09
代码看点有点眼花,建议放在java代码格式内
发表评论

您还没有登录,请登录后发表评论

domain
搜索本博客
最近加入圈子
存档
最新评论
评论排行榜