JR 精品文章 -  用代码学习Spring:IoC、AOP
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » 开发框架 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
 用代码学习Spring:IoC、AOP
Damastes 原创   更新:2006-12-10 12:27:26  版本: 1.0   

1 从http://www.springframework.org下载Spring
2 用eclipse新建Java项目
3 建立我们的业务方法接口
public interface BusinessObject {
    public void doSomething();
    public void doAnotherThing();
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public interface BusinessObject {
    public void doSomething();
    public void doAnotherThing();
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


4 实现业务方法,注意这是的setWords使用了依赖注入,所谓依赖注入就是把配置文件中的字符串什么的在程序运行时“自动”放到我们的程序中来。如果不是这样,我们就只能在代码中固化这些东西,从而违背了面向对象的依赖倒置原则,还有一种满足依赖倒置的方法,即依赖查询,这就是所谓的factory模式,即在代码中请求某种抽象的东西,然后根据配置得到它,但这种办法向对于依赖注入多了对环境的依赖,且代码冗余,EJB的JNDI查询就属于这种。另外我们的Spring配置文件是以bean为核心的,就是我们写的一个类,在XML中描述它的名称、位置和涵盖的内容、关系。
public class BusinessObjectImpl implements BusinessObject {
    private String words;
    public void setWords(String words){
        this.words = words;
    }
    public void doSomething() {
        Log log = LogFactory.getLog(this.getClass());
        log.info(words);
    }
    public void doAnotherThing() {
        Log log = LogFactory.getLog(this.getClass());
        log.info("Another thing");
    }

}public class BusinessObjectImpl implements BusinessObject {
    private String words;
    public void setWords(String words){
        this.words = words;
    }
    public void doSomething() {
        Log log = LogFactory.getLog(this.getClass());
        log.info(words);
    }
    public void doAnotherThing() {
        Log log = LogFactory.getLog(this.getClass());
        log.info("Another thing");
    }

}

5 建立一个运行方法类,从配置文件spring-beans.xml中读入bo这个类的定义,然后实例化一个对象
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;


public class Main {
    public static void main(String[] args){
        XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));
        BusinessObject bo = (BusinessObject)xbf.getBean("bo");
        bo.doSomething();
        bo.doAnotherThing();
    }
}import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;


public class Main {
    public static void main(String[] args){
        XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));
        BusinessObject bo = (BusinessObject)xbf.getBean("bo");
        bo.doSomething();
        bo.doAnotherThing();
    }
}

6 建立一个拦截器类invoke是MethodInterceptor必须实现的方法,表示拦截时的动作,大家仔细体会代码中的含义
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MyInterceptor implements MethodInterceptor {
    private String before, after;
    public void setAfter(String after) {
        this.after = after;
    }
    public void setBefore(String before) {
        this.before = before;
    }
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Log log = LogFactory.getLog(this.getClass());
        log.info(before);
        Object rval = invocation.proceed();
        log.info(after);
        return rval;
    }
}import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MyInterceptor implements MethodInterceptor {
    private String before, after;
    public void setAfter(String after) {
        this.after = after;
    }
    public void setBefore(String before) {
        this.before = before;
    }
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Log log = LogFactory.getLog(this.getClass());
        log.info(before);
        Object rval = invocation.proceed();
        log.info(after);
        return rval;
    }
}

7 建立配置文件组织上面的类之间的关系,AOP有切入点和增强这两个重要的概念,把两个概念结合到一起,就是一个在某个方法执行的时候附加执行,切入点表示在哪里附加,增强表示附加什么,配置文件中的myPointcut表示切入点,myInterceptor表示增强的内容,myAdvisor表示增强器,即两者的结合,在bo这个bean中,我们把这个增强器附加到了bo这个bean上。

<?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="businessObjectImpl" class="BusinessObjectImpl">
        <property name="words">
            <value>正在执行业务方法</value>
        </property>
    </bean>
    <bean id="myInterceptor" class="MyInterceptor">
        <property name="before">
            <value>执行业务方法前</value>
        </property>
        <property name="after">
            <value>执行业务方法后</value>
        </property>
    </bean>
    <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="patterns">
        <list>
            <value>BusinessObject.doSomething</value>
        </list>
    </property>
    </bean>
    <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
      <property name="pointcut" ref="myPointcut"/>
      <property name="advice" ref="myInterceptor"/>
    </bean>
    <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
            <ref local="businessObjectImpl"/>
        </property>
        <property name="proxyInterfaces">
            <value>BusinessObject</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>myInterceptor</value>
                <value>myAdvisor</value>
            </list>
        </property>
    </bean>
</beans><?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="businessObjectImpl" class="BusinessObjectImpl">
        <property name="words">
            <value>正在执行业务方法</value>
        </property>
    </bean>
    <bean id="myInterceptor" class="MyInterceptor">
        <property name="before">
            <value>执行业务方法前</value>
        </property>
        <property name="after">
            <value>执行业务方法后</value>
        </property>
    </bean>
    <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="patterns">
        <list>
            <value>BusinessObject.doSomething</value>
        </list>
    </property>
    </bean>
    <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
      <property name="pointcut" ref="myPointcut"/>
      <property name="advice" ref="myInterceptor"/>
    </bean>
    <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
            <ref local="businessObjectImpl"/>
        </property>
        <property name="proxyInterfaces">
            <value>BusinessObject</value>
        </property>
        <property name="interceptorNames">
            <list>
                <value>myInterceptor</value>
                <value>myAdvisor</value>
            </list>
        </property>
    </bean>
</beans>


8 运行Main类,观察控制台输出结果,重新审查代码,反思为什么会出现这种结果。

版权声明   给作者写信
本篇文章对您是否有帮助?  投票:         投票结果:     18       0
作者其它文章: 作者全部文章
评论人:hyhongyong 发表时间: Mon Dec 11 19:17:43 CST 2006
[good][good]
评论人:Damastes 发表时间: Tue Dec 12 14:16:22 CST 2006
十分抱歉,这个是我在笔记本上写完贴过来的,代码部分被复制了两份,大家看时注意一下,或者去我的blog:
http://www.blogjava.net/yangyi/archive/2006/12/11/87082.html[:)][:)][:)]
评论人:Damastes 发表时间: Tue Dec 12 14:26:37 CST 2006
[:)]   这篇文章是我写在记事本上贴过来的,部分内容贴重复了,大家看时注意一下,或者到我的blog上去看
http://www.blogjava.net/yangyi/archive/2006/12/11/87082.html
评论人:zifeng_java 发表时间: Wed Dec 13 10:11:33 CST 2006
 正式学习中。
评论人:hyhongyong 发表时间: Thu Dec 14 22:08:05 CST 2006
[good][good]
评论人:xzcgeorge 发表时间: Fri Dec 15 10:26:23 CST 2006
[good]
评论人:jiangdadou 发表时间: Tue May 15 10:08:10 CST 2007
[good][good]

这个文章共有 7 条评论
主题: Struts 学习笔记2 -ActionServlet深入探讨 上一篇文章
返回文章列表 返回〔开发框架〕
下一篇文章 主题: Structs简单介绍


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号