安装好SST之后,我们开始来利用Spring写一个Hello World程序。
1. 不用Spring的写法
新建project->新建“com.spring.beans”包->新建“HelloWorld”和“Main”两个类
在HelloWorld.java中输入以下代码:
package com.spring.beans;
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.print("hello:" + name);
}
}
在“Main.java”中输入以下代码:
package com.spring.beans;
public class Main {
public static void main(String[] args) {
//1.创建HellowWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//2.为name属性赋值
helloWorld.setName("world");
//3.调用hello方法
helloWorld.hello();
}
}
执行程序就可得到“hello:world”这个输出。
但当我们使用Spring了之后,第一步和第二步就可以交给Spring来做。具体做法如下:
2. 利用Spring的做法
2.1 先导入包
在网上下载如下包:
- “commons-logging-1.1.3”
- “spring-beans-4.0.0.RELEASE”
- “spring-context-4.0.0.RELEASE”
- “spring-core-4.0.0.RELEASE”
- “spring-expression-4.0.0.RELEASE”
然后再导入project中。
2.2 在“src”下新建Spring的配置文件
右键“src”->“new”->"spring bean configuration file"(如果没有的话点击“other”)进入以下界面
将该文件命名为“applicationContext”,再点击“finish”。
最终project如下:
2.3 配置applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置bean -->
<bean id="helloworld" class="com.spring.beans.HelloWorld"> <!-- class是bean的类名 -->
<property name="name" value="Spring"></property>
</bean>
</beans>
“property”中的“name”代表的是“HelloWorld”类中的“setName”中的“name”:即相当于setName("Spring")。
2.4 修改Main.java代码
Main中的代码修改如下
package com.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.创建HellowWorld的一个对象
//HelloWorld helloWorld = new HelloWorld();
//2.为name属性赋值
//helloWorld.setName("world");
//1.创建Spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Bean实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
//3.调用hello方法
helloWorld.hello();
}
}
运行后结果如下:
红色字体表示的是Spring的日志。
2.5 Spring执行过程
首先先在“HelloWorld”类中加入点输出语句和一个类,修改如下:
package com.spring.beans;
public class HelloWorld {
private String name;
public void setName(String name) {
System.out.print("setName:" + name);
this.name = name;
}
public void hello() {
System.out.println("hello:" + name);
}
public HelloWorld() {
System.out.println("HelloWorld's Constructing..... ");
}
}
再把Main中的2,3步给注释掉,修改如下:
package com.spring.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//1.创建HellowWorld的一个对象
//HelloWorld helloWorld = new HelloWorld();
//2.为name属性赋值
//helloWorld.setName("world");
//1.创建Spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从IOC容器中获取Bean实例
//HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
//3.调用hello方法
//helloWorld.hello();
}
}
执行结果如下:
可以看到“hello:spring”这句话没有输出,所以在创建容器的时候会调用 “HelloWorld”这个构造器并对配置文件中的bean进行初始化,同时会调setName这个方法对name属性进行赋值。