Spring笔记_AOP简单例子

来源:互联网 发布:mac 网络测试工具 编辑:程序博客网 时间:2024/05/21 09:59


接口TestServiceInter

package com.xk.aop;

public interface TestServiceInter
{
    public void sayHello();
}


接口TestServiceInter2

package com.xk.aop;
public interface TestServiceInter2
{
    public void sayBye();
}

前置通知

package com.xk.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice
{
    
    /**
     * method 被调用的方法名
     * args  方法需要的参数
     * target 目标对象
     */

    @Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable
    {
        System.out.println("日志记录。。。"+method.getName());
    }

}

被代理的对象TestService1实现两个接口TestServiceInter,TestServiceInter2

package com.xk.aop;

public class TestService1 implements TestServiceInter,TestServiceInter2
{
    private String name;


        public String getName()
      {
        return name;
      }

     public void setName(String name)
     {
        this.name = name;
    }

     public void sayHello()
     {
        System.out.println("hello: "+name);
     }
    
    @Override
    public void sayBye()
    {
        System.out.println("hello: "+name);
    }
        
}


beans.xml文件放在com.xk.aop目录下

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 配置被代理的对象 -->
<bean id="testService1" class="com.xk.aop.TestService1">
    <property name="name" value="Sawliet"/>
</bean>


<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.xk.aop.MyMethodBeforeAdvice" />

<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean" >

<!-- 代理接口集 -->
    <property name="proxyInterfaces">
    <list>
        <value>com.xk.aop.TestServiceInter</value>
        <value>com.xk.aop.TestServiceInter2</value>
    </list>
    
    </property>

<!-- 把通知织入到代理对象 -->    
    <property name="interceptorNames">
        <value>myMethodBeforeAdvice</value>
    </property>
    
<!-- 配置被代理对象,指定哪些对象被代理 -->
    <property name="target" ref="testService1" />
</bean>

</beans>



测试:

package com.xk.aop;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Test
{
    public static void main(String[] args)
    {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/xk/aop/beans.xml"));
        TestServiceInter inter = (TestServiceInter)factory.getBean("proxyFactoryBean");
        
        inter.sayHello();
        
        ((TestServiceInter2)inter).sayBye();
    }



运行结果:

日志记录。。。sayHello
hello: Sawliet
日志记录。。。sayBye
hello: Sawliet
}