(SPRING 中文网):https://springdoc.cn/inversion-control-and-dependency-injection-in-spring/
Spring 中的控制反转(IoC)和依赖注入(DI)
依赖
<!--spring-context涵盖了aop,beans,core,expression-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<!--测试工具-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
实体类
public class User {
private String id;
private String username;
private String password;
}
public class Student {
private String[] string;
private Object object;
private List<String> list;
private Map<String,String> map;
private Properties properties;
}
配置文件
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--导入外部配置文件-->
<import resource="studentBeans.xml"/>
<!--一个bean代表一个对象,在需要的时候通过反射实例化-->
<!--name可以起多个别名,用","或";"或" "分隔-->
<bean id="user1" class="org.example.pojo.User" name="user1-1;user1-2">
<!--property给对象属性赋值,实际上就是调用set方法,如果没有提供set方法会报错-->
<property name="id" value="-1"/>
<property name="password" value="1234"/>
<property name="username" value="zhangsan"/>
</bean>
<!--有参构造创建对象-->
<bean id="user2" class="org.example.pojo.User">
<constructor-arg name="id" value=""/>
<constructor-arg name="password" value=""/>
<constructor-arg name="username" value=""/>
</bean>
<!--给bean配置别名-->
<alias name="user1" alias="user1-3"/>
<!--p命名空间注入:setter注入-->
<!--singleton:单例模式(默认),这个bean只创建一个对象-->
<!--prototype:原型模式,每次获取这个bean都会产生新对象-->
<bean id="userP" class="org.example.pojo.User" p:username="zhangsanp" p:password="123p" scope="singleton"/>
<!--c命名空间注入:构造器注入-->
<bean id="userC" class="org.example.pojo.User" c:id="1" c:username="zhangsanc" c:password="123c"/>
<!--不同类型依赖注入-->
<bean id="user3" class="org.example.pojo.Student">
<!--对象-->
<property name="object" ref="user1"/>
<!--数组,集合-->
<property name="string">
<array><!--<list>、<set>-->
<value>str1</value>
<value>str2</value>
</array>
</property>
<!--Map-->
<property name="map">
<map>
<entry key="k1" value="v1"/>
<entry key="k2" value="v2"/>
</map>
</property>
<!--null-->
<property name="list">
<null/>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">1234</prop>
</props>
</property>
</bean>
<!--spring的自动装配-->
<!--byName:在容器上下文中查找和类属性名称相同的bean的id-->
<!--byType:在容器上下文中查找和类属性类型相同的bean的class(与属性类型相同的class必须全局唯一)-->
<bean class="org.example.pojo.User" autowire="byName"/>
</beans>
测试类
public class UserTest{
@Test
public void getUser(){
// 传统方式创建对象
// User user = new User();
// IOC:由spring框架自动实例化
BeanFactory beanFactory = new ClassPathXmlApplicationContext("userBeans.xml");
User userC = (User) beanFactory.getBean("userC");
}
}