1. pom文件添加jar包依赖
<dependencies>
<!--依赖于junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--依赖于commons-logging日志管理-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--提供了框架的基本组成部分,包括IOC和DI-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!--提供了BeanFactory-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!--上下文配置对象,提供一个框架式的对象访问方式-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!--提供了强大的表达式语言-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
2. 创建核心配置文件模板
<?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">
</beans>
3. 创建User类
目录:springboot\spring01\src\main\java\com\zzz\pojo\User.java
package com.zzz.pojo;
public class User {
private Integer uid;
private String uname;
private Integer uage;
private String usex;
public Integer getUid() {
return uid;
}
public String getUname() {
return uname;
}
public Integer getUage() {
return uage;
}
public String getUsex() {
return usex;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public void setUname(String uname) {
this.uname = uname;
}
public void setUage(Integer uage) {
this.uage = uage;
}
public void setUsex(String usex) {
this.usex = usex;
}
public User() {
}
public User(Integer uid, String uname, Integer uage, String usex) {
this.uid = uid;
this.uname = uname;
this.uage = uage;
this.usex = usex;
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", uage=" + uage +
", usex='" + usex + '\'' +
'}';
}
}
4. 修改核心配置文件
添加
<bean id="us1" class="com.zzz.pojo.User"/>
<?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="us1" class="com.zzz.pojo.User"/>
</beans>
5. 测试User
目录:springboot\spring01\src\test\java\com\zzz\test\SpringTest.java
package com.zzz.test;
import com.zzz.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test1(){
// 1.加载配置文件
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.获得User对象
User user = (User)app.getBean("us1");
user.setUid(111);
user.setUname("zzz");
user.setUsex("女");
user.setUage(18);
System.out.println(user);
}
}
输出:
十一月 02, 2021 9:49:22 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5679c6c6: startup date [Tue Nov 02 09:49:22 CST 2021]; root of context hierarchy
十一月 02, 2021 9:49:23 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
User{uid=111, uname='zzz', uage=18, usex='女'}