你的位置:首页 > Java教程

[Java教程]spring aop advice


1.前置通知:

import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class HelloBeforeAdvice implements MethodBeforeAdvice{  public void before(Method method, Object[] args, Object target) throws Throwable {    System.out.println("do something before some method");  }}

如果出现 The hierarchy of the type HelloBeforeAdvice is inconsistent错误 , 是缺少相关的jar或者jdk环境不对。

2.后置通知

import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class HelloAfterAdvice implements AfterReturningAdvice {  public void afterReturning(Object result, Method method, Object[] args, Object target) throws Throwable {    System.out.println("do something after the method");  }}

3. Test client

import org.springframework.aop.framework.ProxyFactory;public class TestClient {  public static void main(String[] args) {    ProxyFactory proxyFactory = new ProxyFactory();//创建代理工厂    proxyFactory.setTarget(new HelloImpl());    proxyFactory.addAdvice(new HelloBeforeAdvice());    proxyFactory.addAdvice(new HelloAfterAdvice());    Hello helloProxy = (Hello) proxyFactory.getProxy();    System.out.println(helloProxy.sayHello("zhangsan"));  }}

----------------------------
public class HelloImpl implements Hello {

    public String sayHello(String str) {
        System.out.println("hello>>>"+str);
        return "hello>>>"+str;
    }
}

4. 环绕通知

//aop联盟提供的接口import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class HelloAroundAdvice implements MethodInterceptor {  public Object invoke(MethodInvocation invocation) throws Throwable {    System.out.println("before method>>>");    Object result = invocation.proceed();    System.out.println("after method>>");    return result;  }}

5. spring 配置文件方式

 <??><beans ="http://www.springframework.org/schema/beans" ="http://www.springframework.org/schema/context"    ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/aop"    ="http://www.springframework.org/schema/tx" ="http://www.springframework.org/schema/p"    ="http://www.springframework.org/schema/util" ="http://www.springframework.org/schema/jdbc"    ="http://www.springframework.org/schema/cache"    xsi:schemaLocation="  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx.xsd  http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  http://www.springframework.org/schema/cache  http://www.springframework.org/schema/cache/spring-cache.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd  http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 自动扫描ppms包 ,将带有注解的类 纳入spring容器管理 -->  <context:component-scan base-package="com.cdv.ppms"></context:component-scan>  <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="interfaces" value="com.cdv.ppms.Hello"/><!-- 需要代理的接口 -->    <property name="target" ref="helloImpl"/><!-- 接口实现类 , 此处需要是ref 否则object is not an instance of declaring class-->    <property name="interceptorNames"><!-- 拦截器 名称-->      <list>        <value>helloAroundAdvice</value>      </list>    </property>  </bean>   <bean id="helloImpl" ></bean></beans>

import org.springframework.stereotype.Component;@Componentpublic class HelloImpl implements Hello {//...}@Componentpublic class HelloAroundAdvice implements MethodInterceptor {//....}

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathpublic class Testpublic static void main(String[] args) {    ApplicationContext context = new ClassPath);    Hello  hello = (Hello) context.getBean("helloProxy");    hello.sayHello("test );  }}