spring2 aop 例子

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

首先建立个被切入的类

package sinlff.aop;

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

/**
*姓名
*/
private Stringname;

public void birthday(Stringname) throws Exception{
throw new Exception( " birthday'sExceptionMessage...... " );
}

/** 返回年龄
*
@return theage
*/
public int getAge(){
return age;
}

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

/** 返回姓名
*
@return thename
*/
public StringgetName(){
return name;
}
/** 设置姓名
*
@param stringthenametoset
*/
public void setName(Stringstring){
this .name = string;
}

}

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

package sinlff.aop;

public class BirthdayExecute{

/** 当Person类里的birthday执行抛出异常,则执行的函数
*
@param Exceptionexception类的对象
*/
public void throwExceptionBirthday(Exceptionexception) throws Exception{
System.out.println(exception.getMessage()
+ " throwExceptionBirthdayexecute...... " );
}

/** 当Person类里的birthday执行之前,则执行的函数
*
@param namebirthday函数的字符串参数
*/
public void beforeBirthday(Stringname){
System.out.println(name
+ " beforeBirthdayexecute...... " );
}

/** 当Person类里的birthday执行之后,则执行的函数
*
@param personPerson类的对象
*/
public void afterBirthday(Personperson){
System.out.println(person.getName()
+ " afterBirthdayexecute...... " );
}
}

配置文件

<? xmlversion="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/txhttp://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*(..))andargs(name)" />
< aop:pointcut id ="birthday2"
expression
="execution(*sinlff.aop.Person.birth*(..))andthis(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 Personperson;

public void setUp(){
ApplicationContextapplicationContext
= new ClassPathXmlApplicationContext( " applicationContext.xml " );
person
= (Person)applicationContext.getBean( " person " );
}

public void testSendCardAOP() throws Exception{
person.setName(
" sinlff " );
person.setAge(
22 );
person.birthday(
" newname " );
}
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值