Spring 依赖源码下载地址
https://repo.spring.io/ui/native/release/org/springframework/spring
所需依赖
导入 spring 所需的外部依赖
创建 spring.xml 配置文件
...
public class User {
public void add() {
System.out.println("add...");
}
}
<?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 对象创建
id:定位
class:指定类文件
-->
<bean id="u" class="com.spring5.User"/>
</beans>
...
import com.spring5.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
@Test
public void test() {
// 加载 spring 配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
// 获取配置创建的对象
User user = context.getBean("u", User.class);
System.out.println("user = " + user);
user.add();
}
}
标签:spring,ClassPathXmlApplicationContext,add,user,public,User
From: https://www.cnblogs.com/liuzonglin/p/16736646.html