spring2 aop 例子

本文提供了一个AOP(面向切面编程)的完整实战案例,包括被切入类Person及切入类BirthdayExecute的设计,通过Spring框架进行配置,并展示了如何利用AOP处理方法前、后及异常情况。

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

首先建立个被切入的类

packagesinlff.aop;

/**
*
@authorsinlff
*人实体
*/
publicclassPerson{
/**
*年龄
*/
privateintage;

/**
*姓名
*/
privateStringname;

publicvoidbirthday(Stringname)throwsException{
thrownewException("birthday'sExceptionMessage......");
}

/**返回年龄
*
@returntheage
*/
publicintgetAge(){
returnage;
}

/**设置年龄
*
@paramagetheagetoset
*/
publicvoidsetAge(intage){
this.age=age;
}

/**返回姓名
*
@returnthename
*/
publicStringgetName(){
returnname;
}
/**设置姓名
*
@paramstringthenametoset
*/
publicvoidsetName(Stringstring){
this.name=string;
}

}

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

packagesinlff.aop;

publicclassBirthdayExecute{

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

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

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

配置文件

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/>

<!--被切入的一般操作类-->
<beanid="person"class="sinlff.aop.Person">
</bean>

<!--切入的类-->
<beanid="birthdayExecute"class="sinlff.aop.BirthdayExecute">
</bean>

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

<aop:config>
<aop:pointcutid="birthday1"
expression
="execution(*sinlff.aop.Person.birth*(..))andargs(name)"/>
<aop:pointcutid="birthday2"
expression
="execution(*sinlff.aop.Person.birth*(..))andthis(person)"/>
<aop:pointcutid="birthday3"
expression
="execution(*sinlff.aop.Person.birth*(..))"/>


<aop:advisoradvice-ref="txAdvice"pointcut-ref="birthday1"/>

<aop:aspectid="personBirthday"ref="birthdayExecute">
<aop:beforepointcut-ref="birthday1"
method
="beforeBirthday"arg-names="name"/>
<aop:afterpointcut-ref="birthday2"
method
="afterBirthday"arg-names="person"/>
<aop:after-throwingpointcut-ref="birthday3"
method
="throwExceptionBirthday"throwing="exception"/>
</aop:aspect>
</aop:config>


<beanid="Datesource"
class
="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"
value
="oracle.jdbc.driver.OracleDriver"/>
<propertyname="url"value="jdbc:oracle:thin:@localhost:1521:sinlff"/>
<propertyname="username"value="sinlff"/>
<propertyname="password"value="123456"/>
<propertyname="maxActive">
<value>100</value>
</property>
</bean>
<beanid="SessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="Datesource"/>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<propertyname="mappingResources">
<list>
</list>
</property>
</bean>

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory">
<refbean="SessionFactory"/>
</property>
</bean>

</beans>

测试

packagesinlff.test;

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

importsinlff.aop.Person;


importjunit.framework.TestCase;

publicclassTestAOPextendsTestCase{

privatePersonperson;

publicvoidsetUp(){
ApplicationContextapplicationContext
=newClassPathXmlApplicationContext("applicationContext.xml");
person
=(Person)applicationContext.getBean("person");
}

publicvoidtestSendCardAOP()throwsException{
person.setName(
"sinlff");
person.setAge(
22);
person.birthday(
"newname");
}
}

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值