用代码学习Spring:IoC、AOP

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航