spring中list,set,map集合的设置以及DI(依赖注入)
package com.ning.pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class People {
private int id;
private String name;
private Set<String> sets;
private List<String> list;
private String[] strs;
private Map<String,String> map;
private Desk desk;
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", sets=" + sets +
", list=" + list +
", strs=" + Arrays.toString(strs) +
", map=" + map +
", desk=" + desk +
'}';
}
public People() {
System.out.println("执行构造方法");
}
public People(int id, String name) {
this.id = id;
this.name = name;
System.out.println("执行有参构造");
}
public People(Integer id, String name) {
this.id = id;
this.name = name;
System.out.println("执行有参构造Integer");
}
public People( String name,int id) {
this.id = id;
this.name = name;
System.out.println("执行有参构造1111");
}
public Desk getDesk() {
return desk;
}
public void setDesk(Desk desk) {
this.desk = desk;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public String[] getStrs() {
return strs;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public int getId() {
return id;
}
public void setId(int id) {
System.out.println("执行setid");
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.ning.pojo;
public class Desk {
private int id;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Desk{" +
"id=" + id +
", price=" + price +
'}';
}
}
DI(依赖注入)
di和ioc是一样的,当一个类(A)中需要依赖另一个类(B)对象时,把B赋值给A的过程叫做依赖注入
<?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="peo" class="com.ning.pojo.People">
<property name="id">
<value>666</value>
</property>
<property name="name">
<value>张三</value>
</property>
<property name="sets">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
<value>4</value>
</set>
</property>
<!--<property name="list">-->
<!--<list>-->
<!--<value>1</value>-->
<!--<value>2</value>-->
<!--<value>3</value>-->
<!--</list>-->
<!--</property>-->
<property name="list" value="1,2,3,4,5"/>
<!--<property name="strs">-->
<!--<array>-->
<!--<value>1</value>-->
<!--<value>2</value>-->
<!--<value>3</value>-->
<!--</array>-->
<!--</property>-->
<property name="strs" value="测试1,测试2,测试3"/>
<property name="map">
<map>
<entry key="a" value="b"/>
<entry key="c" value="d"/>
</map>
</property>
<property name="desk" ref="desk"/>
</bean>
<bean id="desk" class="com.ning.pojo.Desk">
<property name="id" value="1"/>
<property name="price" value="8"/>
</bean>
</beans>
测试
package com.ning.test;
import com.ning.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//如何给bean的属性赋值
//通过构造方法设置值
//设置注入(通过set方法) 属性是基本类型或String
public class Test {
public static void main(String[] args) {
// People people = new People();
//对象是在加载这个配置文件的时候就被创建
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//getBean里第一个参数是bean标签里的id值,第二个参数是返回值类型 如果没有第二个参数,默认是object
//getBeanDefinitionNames,spring容器中目前所有管理的所有对象
People peo = ac.getBean("peo", People.class);
System.out.println(peo.getList().size());
System.out.println(peo);
}
}