spring 之集合装配

    当Bean的属性是简单的字面值或者是其他对象的引用,我们可以通过使用“value”和“ref”很方便的满足我们的需要。但是如果Bean的属性是集合呢,那么就需要spring的集合装配。


     首先看下spring提供的可供选择的集合有哪些:

    


来看看如何上手使用:

DepartMent类如下:

public class Department {
	
	public String[] strs = {};
	
	public List<Employee> emps = new ArrayList<Employee>();
	
	public Map<String,Employee> maps = new HashMap<String, Employee>();
	
	public Set<Employee> sets = new HashSet<>();
	
	public Properties props = new Properties();

	// 省略get和set方法
	
}
配置文件如下:

<?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="dept" class="com.csdn.spring.wiring.collection.Department">
		
		<property name="strs">
			<array>
				<value>攻城狮</value>
				<value>程序猿</value>
				<value>测试狗</value>
				<value>产品猪</value>
			</array>
		</property>
		
		<property name="emps">
			<list>
				<ref bean="emp1"/>
				<ref bean="emp2"/>
				<ref bean="emp3"/>
			</list>		
		</property>
		
		<property name="maps">
			<map>
				<entry key="1001" value-ref="emp1" />
				<entry key="1002" value-ref="emp2" />
				<entry key="1003" value-ref="emp3" />
			</map>
		</property>
		
		<property name="sets">
			<set>
				<ref bean="emp1" />
				<ref bean="emp2" />
				<ref bean="emp3" />
			</set>
		</property>
		
		<property name="props">
			<props>
				<prop key="1001">Kobe</prop>
				<prop key="1002">Crawford</prop>
				<prop key="1003">McGrady</prop>
			</props>
		</property>
	</bean>
	
	<bean id="emp1" class="com.csdn.spring.wiring.collection.Employee" />
	<bean id="emp2" class="com.csdn.spring.wiring.collection.Employee" />
	<bean id="emp3" class="com.csdn.spring.wiring.collection.Employee" />
		
</beans>


  • list:装配list类型的值,允许重复
  • set:装配set类型的值,不允许重复
  • map:装配map类型的值,名称和值可以为任意类型
  • props:装配properties类型的值,名称和值必须是Strig型




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring实现自动装配的方式有两个角度:组件扫描和自动装配。组件扫描是指Spring自动发现应用上下文中所创建的bean,而自动装配是指Spring自动满足bean之间的依赖关系,也就是IoC/DI。\[2\] 在Spring中,可以使用@Autowired注解来实现自动装配。通过@Autowired注解,可以将构造器、普通字段和具有参数的方法与相应的bean进行自动装配。默认情况下,所有使用@Autowired注解的属性都需要被设置,如果Spring找不到匹配的bean进行装配,会抛出异常。如果某个属性允许不被设置,可以设置@Autowired注解的required属性为false。当IOC容器中存在多个类型兼容的bean时,Spring会尝试匹配bean的id值是否与变量名相同,如果相同则进行装配,否则可以在@Qualifier注解中提供bean的名称来进行装配。@Autowired注解还可以应用在数组类型的属性和集合属性上,Spring会自动装配所有匹配的bean。当@Autowired注解用在java.util.Map上时,Spring会自动装配与值类型兼容的bean,并以bean的id值作为键。\[1\] 为了启用注解驱动的Bean定义和自动注入策略,需要在Spring配置文件中使用<context:component-scan>元素来启用类扫描机制。这样Spring容器就能够自动扫描并创建带有@Component注解的类,并自动装配它们之间的依赖关系。\[3\] #### 引用[.reference_title] - *1* [Spring之自动装配](https://blog.csdn.net/chen13333336677/article/details/101424191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Spring自动装配](https://blog.csdn.net/qq_41512902/article/details/126140352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值