用代码学习Spring:IoC、AOP(上)

25 篇文章 0 订阅

用代码学习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;
    }
}

  用代码学习Spring:IoC、AOP(下)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值