打开IDEA新建maven工程并引入Spring依赖
在com.ql.spring.ioc.entity包下分别创建Computer、Company实体类
package com.ql.spring.ioc.entity;
public class Computer {
private String brand;
private String type;
private String sn;
private Float price;
public Computer() {
}
public Computer(String brand, String type, String sn, Float price) {
this.brand = brand;
this.type = type;
this.sn = sn;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
@Override
public String toString() {
return "Computer{" +
"brand='" + brand + '\'' +
", type='" + type + '\'' +
", sn='" + sn + '\'' +
", price=" + price +
'}';
}
}
package com.ql.spring.ioc.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Company {
private List<String> rooms;
private Map<String, Computer> computerMap;
private Properties info;
public List<String> getRooms() {
return rooms;
}
public void setRooms(List<String> rooms) {
this.rooms = rooms;
}
public Map<String, Computer> getComputerMap() {
return computerMap;
}
public void setComputerMap(Map<String, Computer> computerMap) {
this.computerMap = computerMap;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Company{" +
"rooms=" + rooms +
", computerMap=" + computerMap +
", info=" + info +
'}';
}
}
在resources目录下创建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">
<bean id="company" class="com.ql.spring.ioc.entity.Company">
<property name="rooms">
<list>
<value>2001-总裁办</value>
<value>2003-总经理办公室</value>
<value>2010-研发部会议室</value>
</list>
</property>
</bean>
</beans>
然后在com.ql.spring.ioc包下创建应用入口类SpringApplication
package com.ql.spring.ioc;
import com.ql.spring.ioc.entity.Company;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Company company = context.getBean("company", Company.class);
System.out.println(company);
}
}
/*
Company{rooms=[2001-总裁办, 2003-总经理办公室, 2010-研发部会议室], computerMap=null, info=null}
*/
通过加断点得知,Spring IoC注入List生成的是ArrayList类型。
List是允许重复的,如果不小心重复添加了数据,就会注入重复的值
可以把当前属性类型改成Set避免重复,Company实体类改为
package com.ql.spring.ioc.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Company {
private Set<String> rooms;
private Map<String, Computer> computerMap;
private Properties info;
public Set<String> getRooms() {
return rooms;
}
public void setRooms(Set<String> rooms) {
this.rooms = rooms;
}
public Map<String, Computer> getComputerMap() {
return computerMap;
}
public void setComputerMap(Map<String, Computer> computerMap) {
this.computerMap = computerMap;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Company{" +
"rooms=" + rooms +
", computerMap=" + computerMap +
", info=" + info +
'}';
}
}
配置文件中再把List标签改成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="company" class="com.ql.spring.ioc.entity.Company">
<property name="rooms">
<set>
<value>2001-总裁办</value>
<value>2003-总经理办公室</value>
<value>2010-研发部会议室</value>
<value>2010-研发部会议室</value>
</set>
</property>
</bean>
</beans>
再次运行就不会重复了
通过加断点得知,Spring IoC注入Set生成的是LinkedHashSet类型,为有序的Set类型。
后面把Map和Properties类型也添加上
<?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="c1" class="com.ql.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="联想"/>
<constructor-arg name="type" value="台式机"/>
<constructor-arg name="sn" value="67867867"/>
<constructor-arg name="price" value="3456"/>
</bean>
<bean id="company" class="com.ql.spring.ioc.entity.Company">
<property name="rooms">
<set>
<value>2001-总裁办</value>
<value>2003-总经理办公室</value>
<value>2010-研发部会议室</value>
<value>2010-研发部会议室</value>
</set>
</property>
<property name="computerMap">
<map>
<entry key="dev-88172" value-ref="c1"/>
<entry key="dev-88173">
<bean class="com.ql.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="惠普"/>
<constructor-arg name="type" value="台式机"/>
<constructor-arg name="sn" value="67867867"/>
<constructor-arg name="price" value="3214"/>
</bean>
</entry>
</map>
</property>
<property name="info">
<props>
<prop key="phone">010-12345678</prop>
<prop key="address">北京海淀区</prop>
<prop key="website">www.xxx.com</prop>
</props>
</property>
</bean>
</beans>
<!--
Company{rooms=[2001-总裁办, 2003-总经理办公室, 2010-研发部会议室], computerMap={dev-88172=Computer{brand='联想', type='台式机', sn='67867867', price=3456.0}, dev-88173=Computer{brand='惠普', type='台式机', sn='67867867', price=3214.0}}, info={phone=010-12345678, address=北京海淀区, website=www.xxx.com}}
-->
通过加断点得知,Spring IoC注入Map生成的是LinkedHashMap类型,为有序的Map类型。
在SpringApplication.java中编写
//获取容器内所有beanId数组
String[] beanNames = context.getBeanDefinitionNames();
for(String beanName: beanNames){
System.out.println(beanName);
}
/*
c1
company
*/
在内部使用的bean不会创建bean id。
如果applicationContext.xml中添加没有id的bean,它自动生成的id为类完整全称+#+序号
<bean class="com.ql.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="微星"/>
<constructor-arg name="type" value="台式机"/>
<constructor-arg name="sn" value="67867867"/>
<constructor-arg name="price" value="3456"/>
</bean>
<bean class="com.ql.spring.ioc.entity.Computer">
<constructor-arg name="brand" value="华硕"/>
<constructor-arg name="type" value="台式机"/>
<constructor-arg name="sn" value="67867867"/>
<constructor-arg name="price" value="3456"/>
</bean>
然后修改SpringApplication.java
package com.ql.spring.ioc;
import com.ql.spring.ioc.entity.Company;
import com.ql.spring.ioc.entity.Computer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.swing.*;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取容器内所有beanId数组
String[] beanNames = context.getBeanDefinitionNames();
for(String beanName: beanNames){
System.out.println(beanName);
System.out.println("类型:"+context.getBean(beanName).getClass().getName());
System.out.println("内容:"+context.getBean(beanName));
}
Computer computer = context.getBean("com.ql.spring.ioc.entity.Computer", Computer.class);
System.out.println(computer.getBrand());
Computer computer1 = context.getBean("com.ql.spring.ioc.entity.Computer#1", Computer.class);
System.out.println(computer1.getBrand());
}
}
/*
c1
类型:com.ql.spring.ioc.entity.Computer
内容:Computer{brand='联想', type='台式机', sn='67867867', price=3456.0}
com.ql.spring.ioc.entity.Computer#0
类型:com.ql.spring.ioc.entity.Computer
内容:Computer{brand='微星', type='台式机', sn='67867867', price=3456.0}
com.ql.spring.ioc.entity.Computer#1
类型:com.ql.spring.ioc.entity.Computer
内容:Computer{brand='华硕', type='台式机', sn='67867867', price=3456.0}
company
类型:com.ql.spring.ioc.entity.Company
内容:Company{rooms=[2001-总裁办, 2003-总经理办公室, 2010-研发部会议室], computerMap={dev-88172=Computer{brand='联想', type='台式机', sn='67867867', price=3456.0}, dev-88173=Computer{brand='惠普', type='台式机', sn='67867867', price=3214.0}}, info={phone=010-12345678, address=北京海淀区, website=www.xxx.com}}
微星
华硕
*/