1.spring5下载repo.spring.io
2.jar包下载Apache Commons Logging - Download Apache Commons Logging
3.打开idea,创建普通java工程,spring5相关jar包添加到自建的lib文件夹中
springframework.core-5.3.13.jar
springframework.beans-5.3.13.jar
spring-context-5.3.13.jar
spring-expression-5.3.13.jar
commons.logging-1.2.jar
4.把jar包导入项目中,file--project structure--Modules--Dependencies,勾选上,点ok
5.开始写代码,先写一个普通的类User,在类中写一个普通的方法
public class User { public void add(){ System.out.println("add:"); } }
6.创建spring配置文件New--XML--Spring Config,命名为bean1.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"> <!--配置User对象创建--> <bean id="身份标识" class="对象路径"> </bean> </beans>
7.测试
@Test public void testAdd(){ //1.加载spring配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); //2.获取配置创建的对象 User a = context.getBean("身份标识", User.class); //3.检测a对象生成 System.out.println(a); a.add(); }
jdk1.8版本以上会出现Error:Cannot determine path to ‘tools.jar’,因为后面的新版本都已经移除了dt.jar和tools.jar