bean的屬性注入3中方式。
1.0 接口注入返咱。
2.0 構(gòu)造函數(shù)注入牍鞠。
3.0 setter方法的注入难述。
spring支持后面的2種注入
示范如下,設(shè)計一個Person類型店读,和一個Student類型攀芯。
Person類,采用setter方法注入屬性殖演。
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
Student類, 采用構(gòu)造函數(shù)注入屬性值趴久。
public class Student {
private Person person;
private String id;
public Student(Person person,String id)
{
this.person=person;
this.id=id;
System.out.println("-construct-------"+id+"student被實列化"+person);
}
public void setPerson(Person person) {
this.person = person;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
xml的配置如下
<bean id="person" class="Person">
<property name="name" value="dflx"></property>
<property name="age" value="999"></property>
</bean>
<bean id="student" class="Student">
<constructor-arg index="0" ref="person"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="123456"></constructor-arg>
</bean>
在main類中彼棍,進行相關(guān)的測試
ApplicationContext app=new ClassPathXmlApplicationContext("helloMessage.xml");
Student student=(Student) app.getBean("student");
System.out.println("http:////////////////");
System.out.println(student.getId());
結(jié)果如下所示
-construct-------123456student被實列化Person [name=dflx, age=999]
////////////////
123456
spring集合類型的屬性注入
list和集合的注入 設(shè)置一個測試類座硕,來展示注入坎吻。
ublic class TestDem {
private ArrayList<String> list;
private String[] str;
public void setList(ArrayList<String> list) {
this.list = list;
}
public void setStr(String[] str) {
this.str = str;
}
@Override
public String toString() {
return "TestDem [list=" + list + ", str=" + Arrays.toString(str) + "]";
}
@Test
public void test()
{
ApplicationContext app=new ClassPathXmlApplicationContext("helloMessage.xml");
TestDem test=(TestDem) app.getBean("test");
System.out.println(test);
}
}
相關(guān)的xml配置如下
<bean id="test" class="TestDem" >
<property name="list">
<list>
<value>alice</value>
<value>jack</value>
</list>
</property>
<property name="str">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</property>
</bean>
最后的結(jié)果如下
TestDem [list=[alice, jack], str=[11, 22, 33]]
Set的注入
xml的相關(guān)配置如下
<bean id="test" class="TestDem" >
<property name="set">
<set>
<value>111</value>
<value>222</value>
</set>
</property>
</bean>
結(jié)果如下
TestDem [set=[111, 222]]
集合屬性map的注入
<bean id="test" class="TestDem" >
<property name="map">
<map>
<entry key="1" value="alice"></entry>
<entry key="2" value="marry"></entry>
</map>
</property>
</bean>
結(jié)果如下
TestDem [map={1=alice, 2=marry}]
Properties的屬性注入
其xml的配置如下
<bean id="test" class="TestDem" >
<property name="proper">
<props>
<prop key="東風(fēng)">冷雪</prop>
<prop key="風(fēng)">雨</prop>
</props>
</property>
</bean>
</beans>
結(jié)果如下
TestDem [proper={東風(fēng)=冷雪, 風(fēng)=雨}]
和小伙伴宇葱,建了一個公眾號瘦真,在摸索中,歡迎關(guān)注黍瞧。搜索公眾號:東風(fēng)冷雪诸尽,英文:satan_master ,現(xiàn)在探索中。無所不有印颤,包括生活您机,學(xué)習(xí),娛樂年局。 三大板塊际看。
公眾號二維碼.jpg