AOP快速入门案例(二)

一 Test1Service
package com.hsp.aop;
public class Test1Service implements TestServiceInter,TestServiceInter2 {
        private String name;
        
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public void sayHello() {
                // TODO Auto-generated method stub
                System.out.println("hi "+name);
        }
        public void sayBye() {
                // TODO Auto-generated method stub
                System.out.println("bye "+name);
        }
}

二 TestServiceInter
package com.hsp.aop;
public interface TestServiceInter {
        public void sayHello();
}

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

四 MyMethodBeforeAdvice
package com.hsp.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
    /**
     * method: 被调用方法名字
     * args: 给method传递的参数
     * target: 目标对象
     */
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("记录日志..."+method.getName());
    }
}

五 App1
package com.hsp.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/aop/beans.xml");
        TestServiceInter ts=(TestServiceInter) ac.getBean("proxyFactoryBean");
        ts.sayHello();
        ((TestServiceInter2)ts).sayBye();
        
    }
}

六 beans.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";
                                >
<!-- 配置被代理的对象 -->
<bean id="test1Service" class="com.hsp.aop.Test1Service">
<property name="name" value="顺平" />
</bean>
<!-- 配置前置通知
        proxyFactoryBean implements TestServiceInter,TestServiceInter2{
                public void sayHello();
        }
        
        思考
        interface Inter1{};
        class A implements Inter1,Inter2{
        }
        Inter1 a=new A();
        Inter2 b=(Inter2)a;
-->
<bean id="MyMethodBeforeAdvice" class="com.hsp.aop.MyMethodBeforeAdvice" />
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理接口集 -->
<property name="proxyInterfaces">
        <list>
                <value>com.hsp.aop.TestServiceInter</value>
                <value>com.hsp.aop.TestServiceInter2</value>
        </list>
</property>
<!-- 把通知织入到代理对象  -->
<property name="interceptorNames">
        <!-- 相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们也
        可以把通知看出拦截器,struts2核心拦截器 -->
        <value>MyMethodBeforeAdvice</value>
</property>
<!-- 配置被代理对象,可以指定 -->
<property name="target" ref="test1Service"/>
</bean>
</beans>

七测试结果
记录日志...sayHello
hi 顺平
记录日志...sayBye
bye 顺平

八AOP术语
1 切面(aspect):要实现的交叉功能,是系统模块化的一个切面或领域。如日志记录。
2 连接点:应用程序执行过程中插入切面的地点,可以是方法调用,异常抛出,或者要修改的字段。
3 通知,切面的实际实现,他通知系统新的行为。如在日志通知包含了实现日志功能的代码,如向日志文件写日志。通知在连接点插入到应用系统中。
4 切入点:定义了通知应该应用在哪些连接点,通知可以应用到AOP框架支持的任何连接点。
5 引入:为类添加新方法和属性。
6 目标对象:被通知的对象。既可以是你编写的类也可以是第三方类。
7 代理:将通知应用到目标对象后创建的对象,应用系统的其他部分不用为了支持代理对象而改变。
8 织入:将切面应用到目标对象从而创建一个新代理对象的过程。织入发生在目标对象生命周期的多个点上:
  编译期:切面在目标对象编译时织入.这需要一个特殊的编译器.
  类装载期:切面在目标对象被载入JVM时织入.这需要一个特殊的类载入器.
  运行期:切面在应用系统运行时织入.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值