服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用代码学习spring:ioc、aop

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

扫描关注微信公众号