快速上手Spring--2.HelloWorld
来源:互联网 发布:java设计模式原理 编辑:程序博客网 时间:2024/05/01 23:06
这篇文章主要谈谈Spring的入门开发,例子很简单,就是输出一条语句。有关各种软件的配置这里就不在多说了,如有不明白的,可以参考我以前的文章。
在开始看这篇文章之前,最好弄清楚IoC、DI等概念,参考《快速上手Spring--1.收集的一些资料》。
一、下载和配置
1. 所需软件
· JDK 5.0
· eclipse-SDK-3.1RC3-win32.zip
· xmlbuddy_2.0.62.zip
· spring-framework-1.2.1-with-dependencies.zip
如果安装后在Eclipse中看不到XmlBuddy,那么在eclipse命令后加上“-clean”参数。
2. 配置
● 配置Eclipse:
· Window->Perferences->Java->Compiler:将"Compiler compliance level"设为“5.0”
· Window->Perferences->Java->Build Path:勾选“Folders”
● 建立库文件夹
具体做法参考《Eclipse快速上手Hibernate--1. 入门实例》一文中的“建立库文件夹”的相关部分。
这里先将spring-framework-1.2.1-with-dependencies.zip解压,将其中的spring.jar(dist目录中)、commons-logging.jar(lib/jakarta-commons目录)、log4j-1.2.9.jar(lib/log4j目录)这三个文件复制到的”D:/java/Spring/lib" 目录中,然后在Eclipse中建立一个“Spring”库,将那三个文件添加进“Spring”库中。
二、项目实践
1. 创建项目
· 新建一个Java Project:SpringHello,注意要导入用户库Spring。
· 这是完成后整个项目的结构(预览一下):
· 项目源码下载(不包含库文件):http://free.ys168.com/?javamxj Spring目录下面。
2. 简单的HelloWorld
·下面开始创建一个新类:HelloWorld ;包名:javamxj.spring.beginning1,代码如下:
HelloWorld.java
package javamxj.spring.beginning1;
public class HelloWorld {
public static void main(String[] args) {
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
}
}
OK!非常简单,我就是要它打印出一条语句“Hello World!”。
现在我不想输出“Hello World!”,我想随心所欲的输出任何语句,很简单啊!将“Hello World!”替换成所希望输出的语句即可。不过这样有一个缺点,每次修改好了,都需要重新编译一下程序。像这样的小程序固然无所谓,可是如果是一个大项目,这样做就有些讨厌了。
还好,只要稍微修改一下程序,通过参数输入即可。
· 继续在这个包下建立一个新类:HelloWorldWithCommandLine,同样非常简单:
HelloWorldWithCommandLine.java
package javamxj.spring.beginning1;
public class HelloWorldWithCommandLine {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("Hello World!");
}
}
}
public class HelloWorldWithCommandLine {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("Hello World!");
}
}
}
· 选中HelloWorldWithCommandLine,右击->Run As->Run...,在弹出窗口切换到“Arguments”栏,在“program arguments”中填入“Hello,javamxj!”,如下图:
这样,就输出了我希望的语句。
第一部分到此,请继续看第二部分。
3. 采用“工厂模式”
· 新建一个包,包名:javamxj.spring.beginning2,在这个包下,新建一个“Hello”接口,这个接口含有一个方法。
Hello.java
package javamxj.spring.beginning2;
public interface Hello {
void sayHello();
} public interface Hello {
void sayHello();
· 分别新建两个类,实现这个接口。
HelloWorld.java
package javamxj.spring.beginning2;
public class HelloWorld implements Hello {
public void sayHello() {
System.out.println("Hello World!");
}
}
public class HelloWorld implements Hello {
public void sayHello() {
System.out.println("Hello World!");
}
}
HelloJavamxj.java
package javamxj.spring.beginning2;
public class HelloJavamxj implements Hello {
public void sayHello() {
System.out.println("Hello, javamxj!");
}
}
public class HelloJavamxj implements Hello {
public void sayHello() {
System.out.println("Hello, javamxj!");
}
}
· 建立一个工厂类,调用上面两个实现接口的子类。
HelloFactory.java
package javamxj.spring.beginning2;
public class HelloFactory {
public Hello getHello(String hello) {
if (hello.equals("world"))
return new HelloWorld();
else if (hello.equals("javamxj"))
return new HelloJavamxj();
else
throw new IllegalArgumentException("输入参数错误!");
}
}
public class HelloFactory {
public Hello getHello(String hello) {
if (hello.equals("world"))
return new HelloWorld();
else if (hello.equals("javamxj"))
return new HelloJavamxj();
else
throw new IllegalArgumentException("输入参数错误!");
}
}
· 最后,利用工厂类,测试一下。
Test.java
package javamxj.spring.beginning2;
public class Test {
public static void main(String[] args) {
Hello hello = null;
HelloFactory factory = new HelloFactory();
hello = factory.getHello("world");
hello.sayHello();
hello = factory.getHello("javamxj");
hello.sayHello();
}
}
public class Test {
public static void main(String[] args) {
Hello hello = null;
HelloFactory factory = new HelloFactory();
hello = factory.getHello("world");
hello.sayHello();
hello = factory.getHello("javamxj");
hello.sayHello();
}
}
运行Test,控制台输出如下语句:
Hello World!
Hello, javamxj!
Hello, javamxj!
4. Setter Injection
讲了这么多,似乎一点都不关Spring的事,别急,下面就看看用Spring是如何实现的。
· 新建一个包,包名:javamxj.spring.beginning3
· 在这个包下,新建一个HelloBean的类,这个类有一个属性:helloWorld属性,这个属性只是一个字符串。可以通过setHelloWorld方法设置这个属性,getHelloWorld方法得到这个属性。
HelloBean.java
package javamxj.spring.beginning3;
public class HelloBean {
private String helloWorld = "Hello!World!";
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
public String getHelloWorld() {
return helloWorld;
}
}
public class HelloBean {
private String helloWorld = "Hello!World!";
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
public String getHelloWorld() {
return helloWorld;
}
}
· 同样,在这个包下,建立一个XML文件,它是Spring配置文件。
bean.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="javamxj.spring.beginning3.HelloBean">
<property name="helloWorld">
<value>Hello! Javamxj!</value>
</property>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="javamxj.spring.beginning3.HelloBean">
<property name="helloWorld">
<value>Hello! Javamxj!</value>
</property>
</bean>
</beans>
Spring配置文件必须遵循spring-beans.dtd定义的内容模型。
这个XML文件在Spring容器中声明了一个HelloBean的实例,并且将它的helloWorld属性设置为“Hello! Javamxj!”。
分析这个XML文件,位于根部的是<beans>元素,它是任何Spring配置文件的根元素。<bean>元素用于告诉Spring容器一个类以及它是如何配置的。这里,id属性用于为Bean helloBean命名,而class属性指定了这个Bean的全限定类名。
在<bean>元素内,<property>元素用于设置一个属性,在本例中它是helloWorld属性。通过使用<property>,我们告诉Spring容器当设置这个属性时调用setHelloWorld方法。greeting属性的值定义在<value>元素内。在这里我设置为“Hello! Javamxj!”。
bean.xml必须在您的CLASSPATH可以存取到的目录中。
· 新建Main.java,测试一下。
Main.java
package javamxj.spring.beginning3;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
// 直接调用HelloBean
HelloBean helloBean = new HelloBean();
System.out.println(helloBean.getHelloWorld());
// 利用Spring调用HelloBean
Resource res = new ClassPathResource("javamxj/spring/beginning3/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
helloBean = (HelloBean) factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
}
}
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
// 直接调用HelloBean
HelloBean helloBean = new HelloBean();
System.out.println(helloBean.getHelloWorld());
// 利用Spring调用HelloBean
Resource res = new ClassPathResource("javamxj/spring/beginning3/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
helloBean = (HelloBean) factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
}
}
这里使用的BeanFactory类就是Spring容器。在将hello.xml文件装入容器后,main方法调用BeanFactory的getBean方法检索出了helloBean服务的一个引用。通过这个引用,它调用了getHelloWorld方法。当我们运行这个Hello应用时,它会打印出“Hello! Javamxj!”。
· 另外,为了更好的调试程序,了解运行机理,建立一个log4j.properties,放在src目录下。
src/log4j.properties
log4j.rootLogger=warn, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n
5. Constructor Injection
· 新建一个包,包名:javamxj.spring.beginning3
· 新建一个HelloBean.java文件,可以与Setter Injection中的HelloBean比较一下,注意是通过Constructor设置helloWorld属性值的。
HelloBean.java
package javamxj.spring.beginning4;
public class HelloBean {
private String helloWorld;
public HelloBean(String helloWorld) {
this.helloWorld = helloWorld;
}
public void sayHello() {
System.out.println(helloWorld);
}
}
public class HelloBean {
private String helloWorld;
public HelloBean(String helloWorld) {
this.helloWorld = helloWorld;
}
public void sayHello() {
System.out.println(helloWorld);
}
}
· 同样,在这个包下,也需要一个Spring配置文件。
bean.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="javamxj.spring.beginning4.HelloBean">
<constructor-arg>
<value>Hello! Javamxj!</value>
</constructor-arg>
</bean>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="javamxj.spring.beginning4.HelloBean">
<constructor-arg>
<value>Hello! Javamxj!</value>
</constructor-arg>
</bean>
</beans>
这里通过<constructor-arg>来表示将使用 constructor injection。如果constructor的参数不只一个,那么需要利用index属性指定参数的位置索引,即<constructor-arg index="索引值">,索引值从0开始。
· 测试一下。
Main.java
package javamxj.spring.beginning4;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
Resource res = new ClassPathResource("javamxj/spring/beginning4/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
HelloBean helloBean = (HelloBean) factory.getBean("helloBean");
helloBean.sayHello();
}
}
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
Resource res = new ClassPathResource("javamxj/spring/beginning4/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
HelloBean helloBean = (HelloBean) factory.getBean("helloBean");
helloBean.sayHello();
}
}
这样,通过BeanFactory的getBean方法,以及xml配置文件,避免了在Main类中直接实例化HelloBean,消除了应用程序(Main)与服务(HelloBean)之间的耦合,实现了IOC(控制反转)或者说实现了依赖的注射(Dependency Injection)。
Setter Injection和Constructor Injection都有自己的长处和不足。至于,是选择Setter Injection,还是选择Constructor Injection?在《Spring in Action》、《Inversion of Control Containers and the Dependency Injection pattern》和《Spring开发指南》都有比较详细的论述,这里就不多说了。
- 快速上手Spring--2.HelloWorld
- 快速上手Spring--2.HelloWorld(1)
- 快速上手Spring--2.HelloWorld(2)
- 快速上手Spring--2.HelloWorld(2)
- 快速上手Spring--2.HelloWorld(1)
- 快速上手Spring--2.HelloWorld(1)
- 快速上手Spring--2.HelloWorld(2)
- 快速上手Spring--2.HelloWorld(1)
- 快速上手Spring--2.HelloWorld(2)
- 快速上手Spring--2.HelloWorld(1)
- 快速上手Spring--1.收集的一些资料
- 快速上手Spring--1.收集的一些资料
- 快速上手Spring--1.收集的一些资料
- 快速上手Spring--1.收集的一些资料
- 快速上手Spring--1.收集的一些资料
- 快速上手Spring--1.收集的一些资料
- 快速上手Spring--收集的一些资料
- 快速上手Spring--1.收集的一些资料
- JSTL(1.1)之Core tag Libray
- wxWidgets & Code::Blocks Studio环境搭建
- 11天..希望会有好运气..
- 快速上手Spring--1.收集的一些资料
- 【转自青年文摘】切中时弊,不可多得
- 快速上手Spring--2.HelloWorld
- JSTL 入门(一): 表达式语言
- 快速上手Spring--3. 加载Bean的配置文件
- 快速上手Spring--4.安装和使用SpringIDE
- 快速上手Spring--5.Bean的标志符(id与name)
- JSTL 入门(二): 表示就是一切
- 快速上手Spring--6. Singleton的使用与否
- 快速上手Spring--7. ref的用法
- JSTL 入门(三): 探讨 core