为阐述清楚Spring IOC框架帮助开发人员完成了那些工作 ,因此从如下实例看:
example 1:
首先实现了FileHelloStr.java:
/**
*
*/
package com.nantian.spring.example1;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author ps
*
*/
public class FileHelloStr {
protected static final Log log = LogFactory.getLog(FileHelloStr.class);
private String profilename;
public FileHelloStr(String profilename) {
this.profilename = profilename;
}
/**
* 读取属性文件
* @return 返回helloworld对应的值
*/
public String getContent(){
String helloWorld = "";
try{
Properties properties = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream(profilename);
properties.load(is);
is.close();
helloWorld = properties.getProperty("helloworld");
}catch (Exception e) {
log.error(e.getMessage());
}
return helloWorld;
}
}
上述类实现对传入的、名为profilename的属性文件的读取工作,并能够打印出相应异常信息。
其次开发者还需要实现HelloWorld类:
如下:
/**
*
*/
package com.nantian.spring.example1;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author ps
*
*/
public class HelloWorld {
protected static final Log log = LogFactory.getLog(HelloWorld.class);
/**
* 读取属性文件
* @return 返回helloworld对应的值
*/
public String getContent(){
FileHelloStr helloStr = new FileHelloStr("helloworld.properties");
return helloStr.getContent();
}
}
上述类调用了FileHelloStr类
开发者还需要实现HelloWorldClient类,从而实现对HelloWorld的调用。
如下:
/**
*
*/
package com.nantian.spring.example1;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author ps
*
*/
public class HelloWorldClient {
protected static final Log log = LogFactory.getLog(HelloWorldClient.class);
/**
* @param args
*/
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
log.info(helloWorld.getContent());
}
}
然后开发者需要编写属性文件(helloworld.properties)其内容如下:
helloworld = "Hello World!"
编写Ant文件运行HelloWorldClient类。
Ant文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="example1" default="run" basedir=".">
<path id="lib">
<fileset dir="d:\learn\框架\struts-2.2.1.1\lib">
<include name="commons-logging-1.0.4.jar" />
</fileset>
</path>
<target name="run" depends="compile" description="Run HelloWorldClient">
<java classname="com.nantian.spring.example1.HelloWorldClient" fork="yes">
<classpath refid="lib"/>
<classpath path="classes"/>
</java>
</target>
<target name="compile">
<mkdir dir="classes"/>
<javac destdir="classes" source="1.7" target="1.7"
deprecation="false" optimize="false" failonerror="true"
includeantruntime="false">
<src path="."/>
<classpath refid="lib"/>
</javac>
<copy todir="classes">
<fileset dir="../../../../">
<include name="helloworld.properties" />
</fileset>
</copy>
</target>
</project>
运行结果:
Buildfile: D:\work\j2ee\workspace\spring1\src\com\nantian\spring\example1\ant.xml
compile:
[javac] Compiling 3 source files to D:\work\j2ee\workspace\spring1\src\com\nantian\spring\example1\classes
run:
[java] 二月 09, 2012 3:28:41 下午 com.nantian.spring.example1.HelloWorldClient main
[java] 信息: "Hello World!"
BUILD SUCCESSFUL
Total time: 1 second
上述过程中,开发者可以看出:HelloWorld明显依赖FileHelloStr,如果开发者需要通过其他途径获取“HelloWorld”信息,则需要重构现有的FileHelloStr类,即通过更通用的HelloStr接口形式给出。一种较好的实现方式是将创建FileHelloStr对象的职责委派给HelloWorldClient客户。
呵呵见下篇。