1.新建一个Java项目
2.下载jar包
下载地址:
(1)https://repo.spring.io/ui/native/release/org/springframework/spring/5.2.6.RELEASE
(2) Central Repository: commons-logging/commons-logging/1.1.1
在项目目录下新建lib文件夹,并将下载的包拷进来并加载
3.编写代码测试
(1)User.java
package com.leo.spring5;
public class User {
public void add() {
System.out.println("add");
}
}
(2)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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置User对象创建-->
<bean id="user" class="com.leo.spring5.User"/>
</beans>
(3)TestSpring5.java
package com.leo.spring5.testdemo;
import com.leo.spring5.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
@Test
public void testAdd() {
// 1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
// 2.获取配置创建的对象
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
}