Spring属性注入

部署运行你感兴趣的模型镜像

#对象类型的注入

值类型value属性
对象类型ref属性

<bean name="user" class="Object.User" init-method="init" destroy-method="destroy">
	<property name="name" value="wuxiangsheng"></property>
	 	<property name="age" value="20"></property>
	 	<property name="car" ref="car"></property>
</bean>
<bean name="car" class="Object.Car">
	 	<property name="name" value="奥迪"></property>
	 	<property name="age" value="1"></property>
</bean>

#Set方式注入

直接在配置文件中指定对象属性的值

<bean name="user" class="Object.User">
	<property name="name" value="wuxiangsheng"></property>
	<property name="age" value="20"></property>
</bean>

#构造方法注入

使用constructor-arg,为构造方法的属性传参。

为了在多个构造方法中确定指定的构造方法,使用type属性和index属性。

<bean name="user" class="Object.User">
	<constructor-arg name="name" value="zhangsan"></constructor-arg>
	<constructor-arg name="age" value="30"></constructor-arg>
</bean>

#p名称空间注入

值类型p:属性名=值
对象类型p:属性名-ref=对象bean名

<!-- 导入额外空间 -->
xmlns:p="http://www.springframework.org/schema/p"
<bean name="user" class="Object.User" p:name="wxs" p:age="10" p:car-ref="car"></bean>

<bean name="car" class="Object.Car">
	<property name="name" value="奥迪"></property>
	<property name="age" value="1"></property>
</bean>

#SPEL(Spring Expression Language)注入

spel注入需要和Set注入或构造方法注入结合使用。

将value值修改为#{Bean对象名.属性}的格式。

注:引用类型没有spel注入,因此仍然使用ref!

<bean name ="user" class="Object.User">
	<property name="name" value="#{user1.name}"></property>
	<property name="age" value="#{user1.age}"></property>
	<property name="car" ref="car"></property>
</bean>

#复杂类型注入

<bean name="cb" class="Object.CollectionBean">
	<!-- 数组类型注入 -->
		<property name="arr">
			<array>
		  		<value>wxs</value>
	  			<value>员工</value>
	  		</array>
	  	</property>
	<!-- List类型注入 -->
		<property name="list">
	  		<list>
	  			<value>zhouyang</value>
	  			<value>清理员</value>
	  		</list>
	  	</property>
	 <!-- Map类型注入 -->
	  	<property name="map">
	  		<map>
	  			<entry key="姓名" value="吴祥生"></entry>
	  			<entry key="年龄" value="20"></entry>
	  			<entry key="汽车" value-ref="car"></entry>
	  		</map>
	  	</property>
	 <!-- properties类型注入 -->
	  	<property name="properties">
	  		<props>
	  			<prop key="url">http://www.jitwxs.cn</prop>
	  			<prop key="name">猿来极客</prop>
	  		</props>
	  	</property>
</bean>
	  	
<bean name="car" class="Object.Car">
	<property name="name" value="奥迪"></property>
	<property name="age" value="1"></property>
</bean>

#相关JavaBean类

//User.java
package Object;

public class User {
	private String name;
	private Integer age;
	private Car car;
	
	public User(){}
	
	public User(String name, Integer age) {
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}

//Car.java
package Object;

public class Car {
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Car [name=" + name + ", age=" + age + "]";
	}
	
}

//CollectionBean.java
package Object;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class CollectionBean {
	private Object[] arr;
	private List list;
	private Map map;
	private Properties properties;
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	public Object[] getArr() {
		return arr;
	}
	public void setArr(Object[] arr) {
		this.arr = arr;
	}
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Properties getProperties() {
		return properties;
	}
	
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
	@Override
	public String toString() {
		return "CollectionBean [arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", properties="
				+ properties + "]";
	}
	
	
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值