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");
    }
}

 

 

 

暫時只給出完整例子,因爲我查到的資料都很零散,而且格式排版都很難看,內容給的不完整,導致學習障礙 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章