网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  用代码学习spring:ioc、aop     
  文章作者:未知  文章来源:水木森林  
  查看:72次  录入:管理员--2007-11-17  
 

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类,观察控制台输出结果,重新审查代码,反思为什么会出现这种结果。

 
 
上一篇: 面向数据字段的表现层组件设计    下一篇: 在eclipse rcp中实现控制反转(ioc)
  相关文档
greedysnake贪吃蛇-测试版 11-17
新手指路灯-jsp程序员成长之路 11-17
j2me移动 2d 图形开发快速入门 11-17
servlet开发初步 11-17
java何以保网络安全 11-17
eclipse插件continuous testing介绍 11-17
理解包(package)的基本概念 11-17
linux安装jdk1.6攻略 11-17
2005年java技术年度综述:融合与开放 11-16
如何用java编写自己的库(3) 11-17
int与bytearray之间的转换程序 11-17
多线程在java me应用程序中的使用 11-17
抢先体验“野马”j2se6.0 11-16
详解java语言中内存泄漏及如何检测问题 (1) 10-20
非常不错的scjp真题回忆 11-17
j2ee全面简介 11-17
java se 6 - jsr 270 最初草案.. 11-17
jsp and xml 11-17
java入门--跟你随便说说字符集和编码 01-11
开发设计模式——asp.net中实现观察者模式 (1) 09-12
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息