spring2 aop 例子

记录下AOP编程常用情况,调整格式,方便阅读

首先建立个被切入的类

package  sinlff.aop;

/**
 * 
@author  sinlff
 * 人实体
 
*/
public   class  Person {
    
/**
     * 年龄
     
*/
    
private   int  age;
    
    
/**
     * 姓名
     
*/
    
private  String name;

    
public   void  birthday(String name)  throws  Exception {
        
throw   new  Exception( " birthday's Exception Message...... " );
    }
    
    
/** 返回年龄
     * 
@return  the age
     
*/
    
public   int  getAge() {
        
return  age;
    }

    
/** 设置年龄
     * 
@param  age the age to set
     
*/
    
public   void  setAge( int  age) {
        
this .age  =  age;
    }

    
/** 返回姓名
     * 
@return  the name
     
*/
    
public  String getName() {
        
return  name;
    }
    
/** 设置姓名
     * 
@param  string the name to set
     
*/
    
public   void  setName(String string) {
        
this .name  =  string;
    }

}

 

要切入Person类的 birthday()方法的类

 

package  sinlff.aop;

public   class  BirthdayExecute {

    
/** 当Person类里的birthday执行抛出异常,则执行的函数
     * 
@param  Exception exception类的对象
     
*/
    
public   void  throwExceptionBirthday(Exception exception)  throws  Exception{  
        System.out.println( exception.getMessage() 
+   "   throwExceptionBirthday execute...... " );
    }
    
    
/** 当Person类里的birthday执行之前,则执行的函数
     * 
@param  name birthday函数的字符串参数
     
*/
     
public   void  beforeBirthday(String name) { 
         System.out.println(name
+ "  beforeBirthday execute...... " );
     }
     
    
/** 当Person类里的birthday执行之后,则执行的函数
     * 
@param  person Person类的对象
     
*/
    
public   void  afterBirthday(Person person) {   
        System.out.println(person.getName() 
+ "   afterBirthday execute...... " );
    }
}

 

配置文件

 

 

 

<? 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"
       xmlns:tx
="http://www.springframework.org/schema/tx"
       xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"

       default-autowire
="byName"  default-lazy-init ="true" >
  
< aop:aspectj-autoproxy />
 
    
<!--  被切入的一般操作类  -->
    
< bean  id ="person"  class ="sinlff.aop.Person" >
    
</ bean >
    
    
<!--  切入的类  -->
    
< bean  id ="birthdayExecute"  class  = "sinlff.aop.BirthdayExecute" >
    
</ bean >

    
< tx:advice  id ="txAdvice"
        transaction-manager
="transactionManager" >
        
< tx:attributes >
            
< tx:method  name ="birth*"  read-only ="true"   />
            
< tx:method  name ="find*"  read-only ="true" />   
            
< tx:method  name ="get*"  read-only ="true" />   
            
< tx:method  name ="list*"  read-only ="true" />  
            
< tx:method  name ="*"  rollback-for ="Exception" />
        
</ tx:attributes >
    
</ tx:advice >

        
< aop:config >
            
< aop:pointcut  id ="birthday1"
                    expression
="execution(* sinlff.aop.Person.birth*(..)) and args(name)"   />
            
< aop:pointcut  id ="birthday2"
                    expression
="execution(* sinlff.aop.Person.birth*(..)) and this(person)"   />
            
< aop:pointcut  id ="birthday3"
                    expression
="execution(* sinlff.aop.Person.birth*(..))"   />
             
             
            
< aop:advisor  advice-ref ="txAdvice"  pointcut-ref ="birthday1"   />      
                     
            
< aop:aspect  id ="personBirthday"  ref ="birthdayExecute" >
                
< aop:before  pointcut-ref ="birthday1"
                    method
="beforeBirthday"  arg-names ="name"   />  
                
< aop:after  pointcut-ref ="birthday2"
                    method
="afterBirthday"  arg-names ="person"   />
                
< aop:after-throwing  pointcut-ref ="birthday3"
                    method
="throwExceptionBirthday"  throwing ="exception"   />
            
</ aop:aspect >
        
</ aop:config >
        

    
< bean  id ="Datesource"
        class
="org.apache.commons.dbcp.BasicDataSource" >
              
< property  name ="driverClassName"
                  value
="oracle.jdbc.driver.OracleDriver"   />
              
< property  name ="url"  value ="jdbc:oracle:thin:@localhost:1521:sinlff"   />
              
< property  name ="username"  value ="sinlff"   />
              
< property  name ="password"  value ="123456"   />
              
< property  name ="maxActive" >
                  
< value > 100 </ value >
        
</ property >
    
</ bean >
    
< bean  id ="SessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  bean ="Datesource"   />
        
</ property >
        
< property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.Oracle9Dialect
                
</ prop >
            
</ props >
        
</ property >
        
< property  name ="mappingResources" >
            
< list >
            
</ list >
        
</ property >
    
</ bean >
    
    
< bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >    
        
< property  name ="sessionFactory" >
            
< ref  bean ="SessionFactory"   />
        
</ property >
    
</ bean >
 
</ beans >

 测试

package  sinlff.test;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

import  sinlff.aop.Person; 
 

import  junit.framework.TestCase;

public   class  TestAOP  extends  TestCase {

    
private  Person person;
    
    
public   void  setUp(){
        ApplicationContext applicationContext 
=   new  ClassPathXmlApplicationContext( " applicationContext.xml " );
        person 
=  (Person) applicationContext.getBean( " person " );    
    }
    
    
public   void  testSendCardAOP()  throws  Exception {
        person.setName(
" sinlff " );
        person.setAge(
22 );
        person.birthday(
" new name " );
    }
}

 

 

 

暂时只给出完整例子,因为我查到的资料都很零散,而且格式排版都很难看,内容给的不完整,导致学习障碍 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值