Java Tutorial/Spring/MethodBeforeAdvice — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 15:22, 31 мая 2010
Implements MethodBeforeAdvice To Check Parameters
File: context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="afterBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="MtBean">
<property name="firstName" value="AAA"/>
</bean>
</property>
<property name="interceptorNames">
<list>
<idref bean="nullArgumentsNotAllowedBeforeAdvice"/>
</list>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
<bean id="nullArgumentsNotAllowedBeforeAdvice"
class="NullArgumentsNotAllowedBeforeAdvice"/>
</beans>
Method Before Advice
File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
public class Main {
public static void main(String[] args) throws Exception {
MessageWriter target = new MessageWriter();
ProxyFactory pf = new ProxyFactory();
pf.addAdvice(new SimpleBeforeAdvice());
pf.setTarget(target);
MessageWriter proxy = (MessageWriter) pf.getProxy();
proxy.writeMessage();
}
}
class MessageWriter {
public void writeMessage() {
System.out.println("A");
}
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("Before method: " + method.getName());
}
}