Java/Spring/MethodBeforeAdvice

Материал из Java эксперт
Перейти к: навигация, поиск

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>

File: Main.java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    MtBean testBean = (MtBean) beanFactory
        .getBean("afterBean");
    testBean.showValues();
  }
}
class MtBean {
  private String firstName;
  public String getFirstName() {
    return this.firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public void showValues() {
    System.out.println("First name: " + this.firstName);
  }
}
class NullArgumentsNotAllowedBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] arguments, Object target)
      throws Throwable {
    if (arguments == null || arguments.length == 0) {
      return;
    }
    
    for (int i = 0; i < arguments.length; i++) {
      Object argument = arguments[i];
      if (argument == null) {
        throw new IllegalArgumentException(
          "Value for argument [" + i + "] is required but not present " +
          "for method [" + method + "]!"
        );
      }
    }
  }
}