一、下载spring5
下载地址:
https://repo.spring.io/ui/native/libs-release-local/org/springframework/spring/5.2.6.RELEASE
下载解压后:
二、创建代码工程
1.idea创建普通java工程:
2.导入spring基础jar包与commons-logging
1)spring基础jar包位置:libs文件夹下
2)工程目录下新建lib目录,拖入上述5个jar包,并加入依赖
三、开始使用spring
1.创建一个类用于测试,必须有无参构造器
package com.my.myClasses;
public class Employee {
public Employee() {
}
public void sout(){
System.out.println("spring test successful!!");
}
}
2.创建spring配置文件
src目录右键–>new–>XML configuration file–>spring config
配置文件内容:
<?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="employee" class="com.my.myClasses.Employee"></bean>
</beans>
3.spring测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.my.myClasses.Employee;
class SpringTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
Employee employee = context.getBean("employee", Employee.class);
System.out.println(employee);
employee.sout();
}
}
执行输出:
com.my.myClasses.Employee@464bee09
spring test successful!!