Spring(一)—— IOC实现2

本文深入解析Spring框架中属性注入的各种方式,包括构造注入、设值注入、p名称空间注入、对象注入、数组和集合注入、Map注入及props注入。通过详细示例,展示了不同注入方式的实现步骤和配置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、属性注入

属性注入:主要指如何给对象中的属性赋值

(一)构造注入:

创建有参构造方法,同时必须提供无参构造
? 1.构造注入:创建Java实体类User

public class User {
	private int id;
	private String name;
	private int age;
	//无参构造和带参构造都必须有
	public User() {
	}
	public User(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

? 2.构造注入:配置applicationContext.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">
 
 	<!-- 注册User对象 -->
 	<!-- 构造方法注入:name或者是用index="0" value="1"...也可以 -->
 	 <bean class="com.sxt.pojo.User">
 		<constructor-arg name="id" value="1" />
 		<constructor-arg name="name" value="tom" />
 		<constructor-arg name="age" value="2" />
 	</bean>
</beans>

? 3.构造注入:Junit测试类测试

public class Test {
	@org.junit.Test
	public void test1(){
		//初始化容器,创建好所有Bean类
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//getBean方法获取对象
		User user = ac.getBean(User.class);
		System.out.println(user);
	}
}

运行结果:
在这里插入图片描述

(二)设值注入:通过setter方法为属性赋值

? 1.设值注入:修改Java实体类User

public class User {
	private int id;
	private String name;
	private int age;
	//必须提供无参构造和setter方法,无参构造创建User对象,setter方法给各个属性赋值
	public User() {
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}	
}

? 2.设值注入:配置applicationContext.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">
 
 	<!-- 注册User对象 -->
 	<!-- 属性注入 -->
 	<bean class="com.sxt.pojo.User">
 		<property name="id" value="3"/>
 		<property name="name" value="tommy"/>
 		<property name="age" value="1"/>
 	</bean>
</beans>

运行结果:测试类与构造注入一致
在这里插入图片描述

(三)p名称空间注入:本质也是setter方法注入

? 1.p名称空间注入:Java实体类User与设值注入一致
? 2.p名称空间注入:配置applicationContext.xml文件
需要在Namespaces中勾选p标签设置,xml文件会自动添加xmlns:p相关的配置信息

<?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对象 -->
 	<!-- p名称空间注入 -->
 	<bean class="com.sxt.pojo.User" p:id="4" p:name="jerry" p:age="5"/>
 </bean>	

运行结果:测试类与构造注入一致
在这里插入图片描述

(四) 对象注入:构造方法、setter方法或p名称空间注入

? 1.设值注入:修改Java实体类User

public class User {
	private int id;
	private String name;
	private int age;
	private Cat cat;//对象
	public User() {
	}
	public User(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", cat=" + cat + "]";
	}
}

? 2.设值注入:配置applicationContext.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 class="com.sxt.pojo.Cat" id="catId">
 		<constructor-arg index="0" value="kitty"/>
 		<constructor-arg index="1" value="blue"/>
 	</bean>
 	
 	<bean class="com.sxt.pojo.User" id="userId">
 		<property name="id" value="6"></property>
 		<property name="name" value="Jack"></property>
 		<property name="age" value="2"></property>
 		<property name="cat" ref="catId"></property>
 	</bean>

运行结果:测试类与构造注入一致
在这里插入图片描述

(五) 数组和集合注入:声明对象,对象中包含数组

? 1.数组和集合注入:修改Java实体类User

public class User {
	private int id;
	private String name;
	private int age;
	private Cat cat;//对象
	private Cat[] cats;//数组
	public User() {
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Cat[] getCats() {
		return cats;
	}
	public void setCats(Cat[] cats) {
		this.cats = cats;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", cats=" + Arrays.toString(cats) + "]";
	}
}

? 2.数组和集合注入:配置applicationContext.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 class="com.sxt.pojo.Cat" id="catId">
 		<constructor-arg index="0" value="kitty"/>
 		<constructor-arg index="1" value="pink"/>
 	</bean>
 	<bean class="com.sxt.pojo.User">
 		<property name="id" value="6"></property>
 		<property name="name" value="Jack"></property>
 		<property name="age" value="2"></property>
 		<property name="cat" ref="catId"></property>
 		<property name="cats">
 			<array>
 				<bean class="com.sxt.pojo.Cat">
					<property name="nick" value="tom1"></property> 				
					<property name="color" value="red"></property> 				
 				</bean>
 				<bean class="com.sxt.pojo.Cat">
					<property name="nick" value="tom2"></property> 				
					<property name="color" value="orange"></property> 				
 				</bean>
 				<bean class="com.sxt.pojo.Cat">
					<property name="nick" value="tom3"></property> 				
					<property name="color" value="yellow"></property> 				
 				</bean>
 			</array>
 		</property>
 	</bean>
</beans>

运行结果:测试类与构造注入一致
在这里插入图片描述

(六) Map注入:

? 1.Map注入:修改Java实体类User

public class User {
	private int id;
	private String name;
	private int age;
	private Cat cat;//对象
	private Map<String,Cat> map;//map集合
	public User() {
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public Map<String, Cat> getMap() {
		return map;
	}
	public void setMap(Map<String, Cat> map) {
		this.map = map;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public User(int id, String name, int age, Cat cat, Map<String, Cat> map) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.cat = cat;
		this.map = map;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", cat=" + cat + ", map=" + map + "]";
	}
}

? 2.Map注入:配置applicationContext.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对象 -->
 	<!-- map集合注入 -->
 	<bean class="com.sxt.pojo.Cat" id="catId">
 		<constructor-arg index="0" value="小白"></constructor-arg>
 		<constructor-arg index="1" value=""></constructor-arg>
 	</bean>
 	<bean class="com.sxt.pojo.User">
 		<property name="id" value="6"></property>
 		<property name="name" value="Jack"></property>
 		<property name="age" value="2"></property>
 		<property name="cat" ref="catId"></property>
 		<property name="map">
 			<map>
 				<entry key="cat1" value-ref="catId"></entry>
 				<entry key="cat2" value-ref="catId"></entry>
 			</map>
 		</property>
 	</bean>
 </beans>

运行结果:测试类与构造注入一致
在这里插入图片描述

( 七)props注入:与Map注入类似

? 1.props注入:修改Java实体类User

public class User {
	private int id;
	private String name;
	private int age;
	private Cat cat;//对象
	private Properties prop;//Properties对象
	public User() {
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Properties getProp() {
		return prop;
	}
	public void setProp(Properties prop) {
		this.prop = prop;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", cat=" + cat + ", prop=" + prop + "]";
	}
}

? 2.props注入:配置applicationContext.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对象 -->
 	<!-- properties注入 -->
 	<bean class="com.sxt.pojo.User">
 		<property name="id" value="6"></property>
 		<property name="name" value="Jack"></property>
 		<property name="age" value="2"></property>
 		<property name="prop">
 			<props>
 				<prop key="username">admin</prop>
 				<prop key="password">123</prop>
 			</props>
 		</property>
 	</bean>
 </beans>

运行结果:测试类与构造注入一致
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值