[03] Spring "Hello World"
0、写在前面的话
本篇以一个简单的示例,描述了Spring通过容器对于Java类的装载和获取。在以下我们可以看到,有一个Java类Coder,我们全程并没有手动调用new来进行实例化,而是从Spring的容器ApplicationContext中去获取到了该类的实例化对象。
1、使用Maven建立Spring项目
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>
7
1
<dependencies>
2
<dependency>
3
<groupId>org.springframework</groupId>
4
<artifactId>spring-context</artifactId>
5
<version>4.3.16.RELEASE</version>
6
</dependency>
7
</dependencies>
使用maven建立项目,按Spring官方提供的dependency,IDE自动会下载关联Spring的相关核心包,如下图:
2、建立一个简单的Java类
public class Coder {
private String name;
private int age;
public void print(){
System.out.println("Hello World");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
24
1
public class Coder {
2
private String name;
3
private int age;
4
5
public void print(){
6
System.out.println("Hello World");
7
}
8
9
public String getName() {
10
return name;
11
}
12
13
public void setName(String name) {
14
this.name = name;
15
}
16
17
public int getAge() {
18
return age;
19
}
20
21
public void setAge(int age) {
22
this.age = age;
23
}
24
}
3、建立Spring的配置文件
对于IDEA而言,pom.xml已经配置了Spring的依赖,那么在新建Spring的配置文件时,可以直接通过IDEA创建最基础的配置文件:
然后配置Java类的bean:
<?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 id="coder" class="dulk.learn.spring.Coder"></bean>
</beans>
x
1
2
<beans xmlns="http://www.springframework.org/schema/beans"
3
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5
6
<bean id="coder" class="dulk.learn.spring.Coder"></bean>
7
8
</beans>
4、初始化容器,获取实例
Spring提供了多种初始化容器的方式,其中最常见的有两种:
- BeanFactory,早期的JavaBean工厂类实现(不推荐)
- ApplicationContext,对BeanFactory的扩展,提供了更多的功能,如国际化处理、Bean自动装配等
BeanFactory:
public class Test {
public static void main(String[] args) {
//BeanFactory
ResourcePatternResolver rpt = new PathMatchingResourcePatternResolver();
Resource resource = rpt.getResource("/applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(resource);
Coder coder = (Coder) bf.getBean("coder");
coder.print();
}
}
10
1
public class Test {
2
public static void main(String[] args) {
3
//BeanFactory
4
ResourcePatternResolver rpt = new PathMatchingResourcePatternResolver();
5
Resource resource = rpt.getResource("/applicationContext.xml");
6
BeanFactory bf = new XmlBeanFactory(resource);
7
Coder coder = (Coder) bf.getBean("coder");
8
coder.print();
9
}
10
}
ApplicationContext:
public class Test {
public static void main(String[] args) {
//ApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
Coder coder = (Coder) ac.getBean("coder");
coder.print();
}
}
1
public class Test {
2
public static void main(String[] args) {
3
//ApplicationContext
4
ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml");
5
Coder coder = (Coder) ac.getBean("coder");
6
coder.print();
7
}
8
}