Spring-集合类型装配

1、新建集合类

CollectionBean.java

/*
 *@Author swxctx
 *@time 2016年9月27日
 */
package com.sw.gather;

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

public class CollectionBean {
	private List myList;
	private Set mySet;
	private Map myMap;
	private Properties myProperties;
	
	public List getMyList() {
		return myList;
	}
	public void setMyList(List myList) {
		System.out.println("List类型:"+myList.getClass().getName());
		this.myList = myList;
	}
	public Set getMySet() {
		return mySet;
	}
	public void setMySet(Set mySet) {
		System.out.println("Set类型:"+mySet.getClass().getName());
		this.mySet = mySet;
	}
	public Map getMyMap() {
		return myMap;
	}
	public void setMyMap(Map myMap) {
		System.out.println("Map类型:"+myMap.getClass().getName());
		this.myMap = myMap;
	}
	public Properties getMyProperties() {
		return myProperties;
	}
	public void setMyProperties(Properties myProperties) {
		System.out.println("Properties类型:"+myProperties.getClass().getName());
		this.myProperties = myProperties;
	}
}
2、配置文件

beans.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-2.5.xsd">
		
		<bean id="collectionBean" class="com.sw.gather.CollectionBean">
			
			<property name="myList">
				<list>
					<value>aa</value>
					<value>bb</value>
				</list>
			</property>
		
			<property name="mySet">
				<set>
					<value>cc</value>
					<value>dd</value>
				</set>
			</property>
			
			<property name="myMap">
				<map>
					<entry key="1" value="ee"></entry>
					<entry key="2" value="ff"></entry>
				</map>
			</property>
			
			<property name="myProperties">
				<props>
					<prop key="3">gg</prop>
					<prop key="4">hh</prop>
				</props>
			</property>
		</bean>	
	
</beans>
3、测试类

/*
 *@Author swxctx
 *@time 2016年9月27日
 */
package com.sw.Test;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sw.gather.CollectionBean;

public class Test {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@org.junit.Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		CollectionBean collectionBean = (CollectionBean)ctx.getBean("collectionBean");
		
		System.out.println("---List---");
		for(Object s:collectionBean.getMyList()){
			System.out.println(s);
		}
		
		System.out.println("---Set---");
		for(Object s:collectionBean.getMySet()){
			System.out.println(s);
		}
		
		System.out.println("---Map---");
		for(Object s:collectionBean.getMyMap().keySet()){
			System.out.println(s+"="+collectionBean.getMyMap().get(s));
		}
		
		System.out.println("---Properties---");
		for(Object s:collectionBean.getMyProperties().keySet()){
			System.out.println(s+"="+collectionBean.getMyProperties().get(s));
		}
	}

}

执行结果如下:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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 ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值