Spring之环境搭建、STS工具和第一个Spring程序
一、下载spring的jar包:
地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-milestone
下载完后,所需jar包:
导入项目中,并添加到类路径:
还需要一个日志jar包:commons logging.jar
下载地址:https://mvnrepository.com/artifact/commons-logging/commons-logging/1.1.1
然后下载Spring-tools工具:
下载地址:http://download.springsource.com/release/STS/3.7.2.RELEASE/dist/e4.5/spring-tool-suite-3.7.2.RELEASE-e4.5.1-win32-x86_64.zip。
下载完解压:打开sts。
将之前的那5个jar包导入javaproject里。
创建Spring Bean Configuration File。
<?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="student" class="org.ruangong.entity.Student"> <property name="stuNo" value="20173681"></property> <property name="stuName" value="靳晨晨"></property> <property name="stuAge" value="21"></property> </bean> </beans>
创建entity类,创建test类:
package org.ruangong.test; import org.ruangong.entity.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args){ // Student student = new Student(); // student.setStuNo(20173681); // student.setStuName("靳晨晨"); // student.setStuAge(21); // System.out.println(student.toString()); ApplicationContext conext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student)conext.getBean("student"); System.out.println(student); } }
完成第一个Spring程序。
Springioc帮助我们New了对象,并且赋了值。