spring aop 之xml
1、类库
2.aop概念
一个切面可以有多个切点
3.在方法前后进行aop的测试代码
3.1aop.xml
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="audience" class="com.lzp.aop.Audience" /> <bean class="com.lzp.aop.Guitar" id="guitar"> <constructor-arg name="name" value="吉他"></constructor-arg> </bean> <bean id="eddie" class="com.lzp.aop.Instrumentalist"> <property name="instrument" ref="guitar" /> </bean> <bean id="testPerformer" class="com.lzp.aop.TestPerformer"></bean> <bean id="logger" class="com.lzp.aop.WriterLogger" /> <bean id="loan" class="com.lzp.aop.TestLoan" /> <bean id="person" class="com.lzp.aop.Person"> <constructor-arg name="name" value="小名"></constructor-arg> </bean> <aop:config> <!--参数执行时间点 --> <aop:aspect ref="audience"> <aop:pointcut expression="execution(* com.lzp.aop.Performer.perform(..))" id="performerPointCut" /> <aop:before pointcut-ref="performerPointCut" method="takeSeats" /> <aop:before pointcut-ref="performerPointCut" method="turnOffCellPhones" /> <aop:after-returning pointcut-ref="performerPointCut" method="applaud" /> <aop:after-throwing pointcut-ref="performerPointCut" method="demandRefund" /> </aop:aspect> <!-- 捕获带参数的方法执行 --> <aop:aspect ref="logger"> <aop:pointcut expression="execution(* com.lzp.aop.Loan.transfer(com.lzp.aop.Person)) and args(person)" id="performerPointCut2" /> <aop:before method="Log" pointcut-ref="performerPointCut2" arg-names="person" /> </aop:aspect> </aop:config> <!--<end id="audience_aspect" /> --> </beans>
3.2 类和接口
//Instrument public abstract class Instrument { public Instrument(String name) { this.name = name; } public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void play(); } //Performer public interface Performer { void perform() throws PerformanceException; } //Instrumentalist public class Instrumentalist implements Performer { public void perform() throws PerformanceException { instrument.play(); } private Instrument instrument; public void setInstrument(Instrument instrument) { this.instrument = instrument; } //Guitar public class Guitar extends Instrument { public Guitar(String name) { super(name); } public void play() { System.out.println(name + "Strum strum strum"); } } public Instrument getInstrument() { return instrument; } }
3.3mainTest
ApplicationContext context = new ClassPathXmlApplicationContext( "aop.xml"); /* Performer performer = (Performer) context.getBean("eddie"); performer.perform();*/
4.获取方法传参的aop测试
4.1xml同上
4.2类和接口代码
public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public interface Loan { void transfer(Person person); }
public class TestLoan implements Loan { @Override public void transfer(Person person) { } }
public interface Logger { void Log(Person Person); Person getPerson(); }
public class WriterLogger implements Logger { private Person person; @Override public void Log(Person person) { // TODO Auto-generated method stub System.out.println("拦截带参数测试:person 的name为"+person.getName()); this.person=person; } @Override public Person getPerson() { // TODO Auto-generated method stub return this.person; } }
上面实例主要记录了获取person类型参数的然后进行记录日志。
mainTest
ApplicationContext context = new ClassPathXmlApplicationContext( "aop.xml"); /* Performer performer = (Performer) context.getBean("eddie"); performer.perform();*/ Loan loan = (Loan) context.getBean("loan"); Person person = (Person) context.getBean("person"); loan.transfer(person);