1.1 IOC容器
步骤1.引入Spring核心jar包的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.9</version> </dependency> </dependencies> <build> <finalName>Spring_Training</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
步骤2.编写Spring相关的xml文件
application_context.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"> <bean id="" class=""> <property name="" value=""/> </bean> </beans>
在application-context.xml文件中,配置bean标签,bean标签主要使用 id 与 class 属性
id:自定义名称,文章后面会讲到用处
class:类所在的全限定路径名
<bean id="" class=""></bean>
在bean标签中,还有内嵌的标签,像是property标签
property标签常用到属性有 name value ref
name:类中的属性名,文章后面会讲到用处
value:给name中基本数据属性赋的值
ref:给name是引用数据类型赋的值
value和ref二选一,只能选一个
<property name="" value=""/>
假设现在,在com.kkk包中有一个名称叫User.class的类
package com.kkk; public class User { private Integer UserId; private String Name; public User() { } public User(Integer userId, String name){ UserId = userId; Name = name; } public void setUserId(Integer UserId) { this.UserId = UserId; } public Integer getUserId() { return this.UserId; } public void setName(String Name) { this.Name = Name; } public String getName() { return this.Name; } @Override public String toString() { return "User{" + "UserId=" + UserId + ", Name='" + Name + '\'' + '}'; } }
而且该类提供了set()和get()方法
如果你想给该类赋值
那么你只需要在之前创建的application_context.xml文件中
按照这几个步骤对xml进行配置
1.配置bean标签
id:可以自定义,我一般是第一个单词小写之后的单词首字母大写
class:该类所在包的全限定路径,本文中是com.kkk包中
<bean id="User" class="com.kkk.User"></bean>
2.在bean标签中配置property标签
property标志中有两个属性
name:需要赋值的类中的属性提供了set()方法,这里面就填写赋值属性对应的属性名
本文章中就是User类中UserId和Name两个字段
value:就是要赋予属性的数值
<bean id="User" class=”com.kkk.User">
<property name="UserId" value="1"/>
<property name="Name" value="kk"/>
</bean>
步骤三:编写测试类UserTest.class
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { public static void main(String[] args) throws BeansException { ApplicationContext ac = new ClassPathXmlApplicationContext("application_context.xml"); User user = (User) applicationContext.getBean("User"); System.out.println(user); } }
首先创建ApplicationContext对象,先通过ClassPathXmlApplicationContext方法传入你创建的xml的文件名,方法返回读取到的ApplicationContext对象
ApplicationContext ac = new ClassPathXmlApplicationContext("application_context.xml");
然后通过ApplicationContext对象的getBean()方法获取到id的数值
User user = (User) applicationContext.getBean("User");
也就是bean标签中id的数值
<bean id="User" class="com.kkk.User"></bean>
最后转换成User类型对象,在控制台打印
System.out.println(user);
查看运行结果:
User{UserId=1, Name='kk'}
ok