本文主要是介绍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;
}
}
/**
* @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...... " );
}
}
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 >
< 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 " );
}
}
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 " );
}
}
暂时只给出完整例子,因为我查到的资料都很零散,而且格式排版都很难看,内容给的不完整,导致学习障碍
这篇关于spring2 aop 例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!