我们通过一个简单程序来演示Spring框架的使用,该入门程序要求在控制台输出”欢迎张三来到Spring”
(1)在IDEA中创建一个名为Spring的Maven项目,然后在pom.xml文件中加载所需使用到的Spring的基础包,即spring-core-5.2.8.RELEASE.jar、spring-beans-5.2.8.RELEASE.jar、spring-context-5.2.8.RELEASE.jar和spring-expression-5.2.8.RELEASE.jar。除此之外,还需将Spring依赖包commons-logging-1.2.RELEASE.jar也加载到项目中。pom.xml文件代码如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
Spring项目的src/main/java目录下下创建一个包这里我创建的是cn.kfu包。
在该包下创建一个HelloSpring的类。在HelloSpring类中定义userName属性和show()方法。HelloSpring类的代码如下图所示。
package cn.kfu;
public class HelloSpring {
private String userName;
public void setUserName(String userName){
this.userName=userName;
}
public void show(){
System.out.println("欢迎"+userName+"来到Spring");
}
}
在Spring项目的src/main/resources目录下,新建applicationContext.xml文件作为HelloSpring类的配置文件,并在applicationContext.xml配置文件中创建id为helloSpring的bean。appliciontContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
" >http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="helloSpring" class="cn.kfu.HelloSpring">
<!--为userName赋值-->
<property name="userName" value="张三"/>
</bean>
</beans>
代码通过<bean>元素配置HelloSpring类,id属性用于标识HelloSpring类的实例名为helloSpring,class属性指定实例化的全路径类名为cn.kfu.HelloSpring,<property>元素为类中的属性赋值,name属性指定为userName,value属性指定userName的值为”张三”。
applicationContext.xml文件包含了许多约束信息,如果手动去写不但浪费时间而且容易编写错误,在Spring的帮助文档中可以找到这些约束信息。
在Spring项目的cn.kfu文件夹下创建测试类TestHelloSpring,在main()方法中初始化Spring容器并加载applicationContext.xml配置文件,通过Spring容器获取HelloSpring类的helloSpring实例,然后调用HelloSpring类中的show()方法在控制台输出信息,TestHelloSpring,类代码如下所示。
import cn.kfu.HelloSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloSpring {
public static void main(String[] args){
//初始化Spring容器,加载applicationContext.xml配置
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器获取配置中helloSpring实例
HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
helloSpring.show();
}
}
启动测试类TestHelloSpring,控制台的输出结果如下图所示。
如图控制台已成功输出了HelloSpring类的show()方法的输出语句,在控制台输出了”欢迎张三来到Spring”。在main()方法中,并没用通过关键字new创建HelloSpring类的对象,而是通过Spring容器获取实体类对象,这就是Spring IoC容器的实现机制。