关于Spring注入集合对象的用法(有示例!全网最详细!!)
如果觉得写的还可以,点个赞支持一下笔者呗!笔者会持续更新关于Java和大数据有关的文章
1.注入List
下图是配置文件bean的设置
2.注入Set
下图是配置文件bean的设置
注意点:set底层存储形式为LinkedHashSet,且会去重,举个例子如果在set里面存入多个相同的值或对象,set只会保留一个。
<propertyname="rooms"><set><value>2001-总裁办</value><value>2003-总经理办公室</value><value>2010-研发部会议室</value><value>2010-研发部会议室</value></set></property>
3.注入Map
注意点:采用的LinkedHashMap的方式,也就是说存储数据的顺序和提取数据的顺序是相同的
4.注入Properties
案例:
pom.xml
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.imooc.spring</groupId><artifactId>ioc</artifactId><version>1.0-SNAPSHOT</version><repositories><repository><id>aliyun</id><name>aliyun</name><url>https://maven.aliyun.com/repository/public</url></repository></repositories><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.6.RELEASE</version></dependency></dependencies></project>
applicationContest.xml
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="c1"class="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="联想"/><constructor-argname="type"value="台式机"/><constructor-argname="sn"value="8389283012"/><constructor-argname="price"value="3085"/></bean><beanclass="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="微星"/><constructor-argname="type"value="台式机"/><constructor-argname="sn"value="8389280012"/><constructor-argname="price"value="3000"/></bean><beanclass="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="华硕"/><constructor-argname="type"value="笔记本"/><constructor-argname="sn"value="9089380012"/><constructor-argname="price"value="6000"/></bean><beanid="company"class="com.imooc.spring.ioc.entity.Company"><propertyname="rooms"><set><value>2001-总裁办</value><value>2003-总经理办公室</value><value>2010-研发部会议室</value><value>2010-研发部会议室</value></set></property><propertyname="computers"><map><entrykey="dev-88172"value-ref="c1"/><entrykey="dev-88173"><beanclass="com.imooc.spring.ioc.entity.Computer"><constructor-argname="brand"value="联想"/><constructor-argname="type"value="笔记本"/><constructor-argname="sn"value="1280258012"/><constructor-argname="price"value="5060"/></bean></entry></map></property><propertyname="info"><props><propkey="phone">010-12345678</prop><propkey="address">北京市朝阳区XX路XX大厦</prop><propkey="website">http://www.xxx.com</prop></props></property></bean></beans>
Company实体类
importjava.util.List; importjava.util.Map; importjava.util.Properties; importjava.util.Set; publicclassCompany { privateSet<String>rooms; privateMap<String,Computer>computers; privatePropertiesinfo; publicSet<String>getRooms() { returnrooms; } publicvoidsetRooms(Set<String>rooms) { this.rooms=rooms; } publicMap<String, Computer>getComputers() { returncomputers; } publicvoidsetComputers(Map<String, Computer>computers) { this.computers=computers; } publicPropertiesgetInfo() { returninfo; } publicvoidsetInfo(Propertiesinfo) { this.info=info; } publicStringtoString() { return"Company{"+"rooms="+rooms+", computers="+computers+", info="+info+'}'; } }
Computer实体类
publicclassComputer { privateStringbrand; privateStringtype; privateStringsn; privateFloatprice; publicComputer() { } publicComputer(Stringbrand, Stringtype, Stringsn, Floatprice) { this.brand=brand; this.type=type; this.sn=sn; this.price=price; } publicStringgetBrand() { returnbrand; } publicvoidsetBrand(Stringbrand) { this.brand=brand; } publicStringgetType() { returntype; } publicvoidsetType(Stringtype) { this.type=type; } publicStringgetSn() { returnsn; } publicvoidsetSn(Stringsn) { this.sn=sn; } publicFloatgetPrice() { returnprice; } publicvoidsetPrice(Floatprice) { this.price=price; } publicStringtoString() { return"Computer{"+"brand='"+brand+'\''+", type='"+type+'\''+", sn='"+sn+'\''+", price="+price+'}'; } }
springApplication应用类
importcom.imooc.spring.ioc.entity.Company; importcom.imooc.spring.ioc.entity.Computer; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassSpringApplication { publicstaticvoidmain(String[] args) { ApplicationContextcontext=newClassPathXmlApplicationContext("classpath:applicationContext.xml"); Companycompany=context.getBean("company", Company.class); System.out.println(company); Stringwebsite=company.getInfo().getProperty("website"); System.out.println(website); System.out.println("=============================="); //获取容器内所有beanId数组String[] beanNames=context.getBeanDefinitionNames(); for (StringbeanName:beanNames){ System.out.println(beanName); System.out.println("类型:"+context.getBean(beanName).getClass().getName()); System.out.println("内容:"+context.getBean(beanName)); } Computercomputer=context.getBean("com.imooc.spring.ioc.entity.Computer", Computer.class); System.out.println(computer.getBrand()); Computercomputer1=context.getBean("com.imooc.spring.ioc.entity.Computer#1", Computer.class); System.out.println(computer1.getBrand()); } }