数组注入
import java.util.Arrays;
public class TestBean {
private String name;
private Integer age;
private Computer[] computers;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setComputers(Computer[] computers) {
this.computers = computers;
}
@Override
public String toString() {
return "TestBean{" +
"name='" + name + '\'' +
", age=" + age +
", computers=" + Arrays.toString(computers) +
'}';
}
}
<bean name="c1" class="domain.Computer"></bean>
<bean name="c2" class="domain.Computer"></bean>
<bean name="c3" class="domain.Computer"></bean>
<bean id="testBean" class="domain.TestBean">
<property name="name" value="zhangfei"></property>
<property name="age" value="19"></property>
<property name="computers">
<array value-type="domain.Computer">
//嵌套方法
<bean class="domain.Computer">
<property name="name" value="小米"></property>
</bean>
<bean class="domain.Computer">
<property name="name" value="华为"></property>
</bean>
<bean class="domain.Computer">
<property name="name" value="苹果"></property>
</bean>
//引用方法
<ref bean="c1"></ref>
<ref bean="c2"></ref>
<ref bean="c3"></ref>
</array>
</property>
</bean>
BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestBean bean = (TestBean) factory1.getBean("testBean");
System.out.println(bean);
- 运行结果(因为嵌套的对象没有赋值,输出的结果都是null)
TestBean{name='zhangfei', age=19, computers=[Computer{brand='null', name='小米', price=null}, Computer{brand='null', name='华为', price=null}, Computer{brand='null', name='苹果', price=null}, Computer{brand='null', name='null', price=null}, Computer{brand='null', name='null', price=null}, Computer{brand='null', name='null', price=null}]}
list注入(非自定义类型)
- 一个有list的实体类,配置如下:(将上面数组改为list)
- 配置文件如下:
<bean name="testBean" class="domain.TestBean">
<constructor-arg name="name" value="machao" type="java.lang.String"></constructor-arg>
<constructor-arg name="age" value="25" type="java.lang.Integer"></constructor-arg>
<constructor-arg name="liststring" type="java.util.List">
<list value-type="java.lang.String">
<value>1111</value>
<value>aaaa</value>
<value>cccc</value>
<value>dddd</value>
</list>
</constructor-arg>
</bean>
BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestBean bean = (TestBean) factory1.getBean("testBean");
System.out.println(bean);
- 运行结果(因为嵌套的对象没有赋值,输出的结果都是null)
TestBean{name='machao', age=25, liststring=[1111, aaaa, cccc, dddd]}
list注入(自定义类型)
- 一个有list的实体类,配置如下:(将上面list<String>改为list<Computer)
- 配置文件如下:(包含了嵌套和外部引用的两种方法)
<bean name="c1" class="domain.Computer"></bean>
<bean id="testBean" class="domain.TestBean">
<constructor-arg name="name" value="huangzhogn" type="java.lang.String"></constructor-arg>
<constructor-arg name="age" value="224"></constructor-arg>
<constructor-arg name="listcomputer" type="java.util.List">
<list value-type="domain.Computer">
<bean class="domain.Computer">
<property name="name" value="17pro"></property>
<property name="brand" value="小米"></property>
<property name="price" value="9999"></property>
</bean>
<bean class="domain.Computer"></bean>
<bean class="domain.Computer"></bean>
<ref bean="c1"></ref>
</list>
</constructor-arg>
</bean>
BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestBean bean = (TestBean) factory1.getBean("testBean");
System.out.println(bean);
TestBean{name='huangzhogn', age=224, listcomputer=[Computer{brand='小米', name='17pro', price=9999.0}, Computer{brand='null', name='null', price=null}, Computer{brand='null', name='null', price=null}, Computer{brand='null', name='null', price=null}]}
set注入(基本类型、对象、set)
- 一个有String、Computer、Set的实体类,配置如下:
public class TestBean {
private String name;
private Computer computer;
private Set<String> setstring;
public void setName(String name) {
this.name = name;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
public void setSetstring(Set<String> setstring) {
this.setstring = setstring;
}
public TestBean(String name, Computer computer, Set<String> setstring) {
this.name = name;
this.computer = computer;
this.setstring = setstring;
}
@Override
public String toString() {
return "TestBean{" +
"name='" + name + '\'' +
", computer=" + computer +
", setstring=" + setstring +
'}';
}
}
<bean id="testBean" class="domain.TestBean">
<constructor-arg name="name" type="java.lang.String">
<value>guanyu</value>
</constructor-arg>
<constructor-arg name="computer" type="domain.Computer">
<bean class="domain.Computer"></bean>
</constructor-arg>
<constructor-arg name="setstring" type="java.util.Set">
<set value-type="java.lang.String">
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</constructor-arg>
</bean>
BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestBean bean = (TestBean) factory1.getBean("testBean");
System.out.println(bean);
TestBean{name='guanyu', computer=Computer{brand='null', name='null', price=null}, setstring=[aaa, bbb, ccc]}
Map注入(基本类型、对象、map)
private String name;
private Computer computer;
// private Set<String> setstring;
private Map<Integer,String> map;
set 方法 有参 无参构造方法...
<bean id="testBean" class="domain.TestBean">
<property name="name" value="zhaoyun"></property>
<property name="computer">
<bean class="domain.Computer"></bean>
</property>
<property name="map">
<map key-type="java.lang.Integer" value-type="java.lang.String">
<entry key="1" value="aaa"></entry>
<entry key="2" value="bbb"></entry>
<entry key="3" value="ccc"></entry>
</map>
</property>
</bean>
BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestBean bean = (TestBean) factory1.getBean("testBean");
System.out.println(bean);
TestBean{name='zhaoyun', computer=Computer{brand='null', name='null', price=null}, map={1=aaa, 2=bbb, 3=ccc}}
Map注入(自定义类型)
private String name;
private Computer computer;
// private Set<String> setstring;
private Map<Integer,String> map;
set 方法 有参 无参构造方法...
<bean id="testBean" class="domain.TestBean">
<property name="name" value="zhaoyun"></property>
<property name="computer">
<bean class="domain.Computer"></bean>
</property>
<property name="map">
<map key-type="java.lang.Integer" value-type="domain.Computer">
<entry key="1">
<bean class="domain.Computer">
<property name="brand" value="小米"></property>
<property name="price" value="18888"></property>
<property name="name" value="28pro"></property>
</bean>
</entry>
<entry key="2">
<bean class="domain.Computer">
<property name="brand" value="华为"></property>
<property name="price" value="13888"></property>
<property name="name" value="30pro"></property>
</bean>
</entry>
<entry key="3">
<bean class="domain.Computer">
<property name="brand" value="外星人"></property>
<property name="price" value="19999"></property>
<property name="name" value="40pro"></property>
</bean>
</entry>
</map>
</property>
</bean>
BeanFactory factory1 = new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestBean bean = (TestBean) factory1.getBean("testBean");
System.out.println(bean);
TestBean{name='zhaoyun', computer=Computer{brand='null', name='null', price=null}, map={1=Computer{brand='小米', name='28pro', price=18888.0}, 2=Computer{brand='华为', name='30pro', price=13888.0}, 3=Computer{brand='外星人', name='40pro', price=19999.0}}}
网友评论