记录下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;
}
}
/**
*@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......");
}
}
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>
<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");
}
}
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");
}
}
暂时只给出完整例子,因为我查到的资料都很零散,而且格式排版都很难看,内容给的不完整,导致学习障碍
本文提供了一个AOP(面向切面编程)的完整实战案例,包括被切入类Person及切入类BirthdayExecute的设计,通过Spring框架进行配置,并展示了如何利用AOP处理方法前、后及异常情况。
54万+

被折叠的 条评论
为什么被折叠?



