`

Spring 程序示例

阅读更多

创建 Spring 程序简单程序:

一、首先建立目录myspring,该目录下分别建立以下子目录:
build -> 存放编译文件class
lib -> 存放库文件spring.jar,commons-logging.jar
src -> 存放java源文件

二、在myspring/src目录下创建java源文件:
touch Message.java Information.java Display.java Console.java Instance.java beans.xml

接口:Message ->类:Information 提供信息
接口:Display -> 类: Console 显示信息
启动类:Instance
配置文件:beans.xml

三、myspring/src目录下的代码如下:

Message.java

public interface Message ... {
public StringgetString(); // 接口方法
}


Information.java

public class Information implements Message ... {
public StringgetString() ... { // 实现接口方法
return " Helloworld,ThisisaSpringdemo. " ;

}

}


Display.java

public interface Display ... {
// 接口方法,显示消息
public void display();
// 接口定义了属性message,访问Message接口
public void setMessage(Messagemessage);
public MessagegetMessage();
}


Console.java

public class Console implements Display ... {
private Messagemessage = null ; // 定义message属性

public void display() ... { // 实现接口方法,显示消息
if (message == null ) ... {
throw new RuntimeException( " Yourmustsetpropertymessageofclass: " +
Console. class .getName());
}

System.out.println(message.getString());
}


public void setMessage(Messagemessage) ... {
this .message = message;
}


public MessagegetMessage() ... {
return message;
}

}


Instance.java

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class Instance ... {
public static void main(String[]args) ... {
XmlBeanFactoryfactory
= new XmlBeanFactory( new FileSystemResource( " build/beans.xml " ));
Consoleconsole
= (Console)factory.getBean( " console " ); // 获取Console类实例
console.display(); // 使用接口约定的方法,显示消息。
}

}



Instance类使用的bean由beans.xml指定,Spring通过beans.xml配置向Console类注入了Information类,保存在Console类的message属性事,以便Console在调用display时取得消息并显示。

beans.xml

<! DOCTYPEbeansPUBLIC"-//SPRING//BEANDTD//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"
>
< beans >
< bean id ="information" class ="Information" />
< bean id ="console" class ="Console" >
< property name ="message" >
< ref local ="information" />
</ property >
</ bean >
</ beans >



四、在myspring主目录下创建项目文件build.xml:

< project name ="myspring" default ="run" basedir ="." >
< property name ="srcdir" value ="${basedir}/src" />
< property name ="libdir" value ="${basedir}/lib" />
< property name ="destdir" value ="${basedir}/build" />

< path id ="libraries" >
< fileset dir ="${libdir}" >
< include name ="*.jar" />
</ fileset >
</ path >

< target name ="clean" >
< delete dir ="${destdir}" />
< mkdir dir ="${destdir}" />
</ target >

< target name ="configuration" >
< copy todir ="${destdir}" >
< fileset dir ="${srcdir}" >
< exclude name ="**/*.java" />
</ fileset >
</ copy >
</ target >

< target name ="compile" depends ="configuration" >
< javac srcdir ="${srcdir}" destdir ="${destdir}" classpathref ="libraries" />
</ target >

< target name ="run" depends ="compile" >
< java classname ="Instance" fork ="true" classpathref ="libraries" >
< classpath path ="${destdir}" />
</ java >
</ target >
</ project >



五、在myspring主目录下执行ant
ant必须在路径上能搜索到,否则不能执行,执行结果:
run:
[java] 2008-2-3 7:58:46 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
[java] 信息: Loading XML bean definitions from file [/home/jones/workspace/myspring/build/beans.xml]
[java] Hello world, This is a Spring demo.


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics