Spring Aop 例子(Spring in action)

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="audience" class="com.springinaction.springidol.Audience"></bean> <bean id="audienceAdvice" class="com.springinaction.springidol.AudienceAdvice"> <property name="audience" ref="audience"></property> </bean> <bean id="performancePointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*perform"></property> </bean> <bean id="audienceAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="audienceAdvice"></property> <property name="pointcut" ref="performancePointcut"></property> </bean> <bean id="dukeTarget" class="com.springinaction.springidol.PoeticJuggler"> <property name="name" value="huguodong"></property> </bean> <bean id="duke" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="dukeTarget"></property> <property name="interceptorNames" value="audienceAdvisor"></property> <property name="proxyInterfaces" value="com.springinaction.springidol.Performer"></property> </bean> </beans>

 

package com.springinaction.springidol; public class Audience { public Audience() { } public void takeSeats() { System.out.println("taking their seats"); } public void turnOffCellPhone() { System.out.println("turning off their cellphones"); } public void appaud() { System.out.println("CLAP CLAP CLAP CLAP CLAP"); } public void demandRefund() { System.out.println("Boo! We want our money back!"); } }

package com.springinaction.springidol; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.beans.factory.annotation.Autowired; public class AudienceAdvice implements MethodBeforeAdvice ,AfterReturningAdvice { @Autowired private Audience audience; public Audience getAudience() { return audience; } public void setAudience(Audience audience) { this.audience = audience; } @Override public void before(Method method, Object[] args, Object target) throws Throwable { audience.takeSeats(); audience.turnOffCellPhone(); } @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { // TODO Auto-generated method stub System.out.println("after return"); } }

package com.springinaction.springidol; public interface Performer { void perform(); void aaaperform(); }

package com.springinaction.springidol; public class PoeticJuggler implements Performer { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void perform() { // TODO Auto-generated method stub System.out.println(this.name + "poetic Juggler perform"); } public void aaaperform(){ System.out.println("aaaaperform"); } }

你可能感兴趣的:(spring,AOP,object,Class,action,Autowired)