spring-autowire、集合等各种参数注入
本文参考书《轻量级javaee实战》
说明:autowire中的byName和byType分别代表通过名字和通过类型进行装配。
byName需要类中有对应的setter方法,例如Parent中有对应的setChild();那么就会装配id为child的bean。
byType需要类中成员的类型在xml中出现有且仅有一次,否则会出现一场。例如:Body中有private Leg leg;那么根据byType的autowire就会找到class为Leg的bean进行装配。
对应如何进行集合类的装配可以看代码,对于集合类的概念不清晰的,可以参考《疯狂java讲义》。
下面是本例子的代码:
<bean id="example" class="com.xueyoucto.xueyou.Example">
<property name="age" value="12"/>
<property name="name" value="xx"/>
</bean>
<bean id="student" class="com.xueyoucto.xueyou.Student">
<constructor-arg index="0" value="mmm"/>
<constructor-arg index="1" value="4"/>
</bean>
<bean id="parent" class="com.xueyoucto.xueyou.Parent" autowire="byName">
<property name="name" value="pppppppppp"/>
</bean>
<bean id="child" class="com.xueyoucto.xueyou.Child">
<property name="age" value="12"/>
<property name="name" value="xx"/>
</bean>
<bean id="body" class="com.xueyoucto.xueyou.Body" autowire="byType">
<property name="name" value="body111"/>
</bean>
<bean id="leg" class="com.xueyoucto.xueyou.Leg">
<property name="name" value="leg1"/>
<property name="length" value="10"/>
</bean>
<bean id="chinese" class="com.xueyoucto.xueyou.Chinese">
<property name="schools">
<list>
<value>小学</value>
<value>小学1</value>
<value>小学2</value>
<value>小学3</value>
</list>
</property>
<property name="scores">
<map>
<entry key="数学" value="78"/>
<entry key="语文" value="87"/>
<entry key="英语" value="99"/>
<entry key="政治" value="18"/>
</map>
</property>
<property name="phaseAxes">
<map>
<entry key="原始" value-ref="axe"></entry>
<entry key="现代" value-ref="axe"></entry>
</map>
</property>
<property name="health">
<props>
<prop key="血压">正常</prop>
<prop key="体重">70kg</prop>
<prop key="血型">A型</prop>
</props>
</property>
<property name="axes">
<set>
<value>普通字符串</value>
<bean class="com.xueyoucto.xueyou.Axe"></bean>
<ref bean="axe2"/>
<list>
<value>20</value>
<set>
<value type="int">30</value>
</set>
</list>
</set>
</property>
<property name="books">
<list>
<value>书名1</value>
<value>书名2</value>
<value>书名3</value>
<value>书名4</value>
</list>
</property>
</bean>
<bean id="axe" class="com.xueyoucto.xueyou.Axe" scope="prototype">
<property name="name" value="普通斧子"></property>
</bean>
<bean id="axe2" class="com.xueyoucto.xueyou.Axe2">
<property name="name" value="高级斧子"></property>
</bean>
Axe.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
public class Axe {
private String name;
public Axe() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Axe2.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
public class Axe2 {
private String name;
public Axe2() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Body.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
import com.xueyoucto.xueyou.Leg;
public class Body {
private Leg leg;
private String name;
public Body() {
}
public Leg getLeg() {
return this.leg;
}
public void setLeg(Leg leg) {
this.leg = leg;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
public class Child {
private String name;
private int age;
public Child() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
Chinese.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
import com.xueyoucto.xueyou.Axe;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Chinese {
private List<String> schools;
private Map scores;
private Map<String, Axe> phaseAxes;
private Properties health;
private Set axes;
private String[] books;
public Chinese() {
System.out.println("实例化Chinese");
}
public List<String> getSchools() {
return this.schools;
}
public void setSchools(List<String> schools) {
this.schools = schools;
}
public Map getScores() {
return this.scores;
}
public void setScores(Map scores) {
this.scores = scores;
}
public Map<String, Axe> getPhaseAxes() {
return this.phaseAxes;
}
public void setPhaseAxes(Map<String, Axe> phaseAxes) {
this.phaseAxes = phaseAxes;
}
public Properties getHealth() {
return this.health;
}
public void setHealth(Properties health) {
this.health = health;
}
public Set getAxes() {
return this.axes;
}
public void setAxes(Set axes) {
this.axes = axes;
}
public String[] getBooks() {
return this.books;
}
public void setBooks(String[] books) {
this.books = books;
}
public void test() {
System.out.println(this.schools);
System.out.println(this.scores);
System.out.println(this.phaseAxes);
System.out.println(this.health);
System.out.println(this.axes);
System.out.println(Arrays.toString(this.books));
}
}
Example.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
public class Example {
private int age;
private String name;
public Example() {
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Leg.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
public class Leg {
private int length;
private String name;
public Leg() {
}
public int getLength() {
return this.length;
}
public void setLength(int length) {
this.length = length;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Parent.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
import com.xueyoucto.xueyou.Child;
public class Parent {
private String name;
private Child child;
public Parent() {
}
public Child getChild() {
return this.child;
}
public void setChild(Child child) {
this.child = child;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
Student.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}
主程序:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xueyoucto.xueyou;
import com.xueyoucto.xueyou.Body;
import com.xueyoucto.xueyou.Chinese;
import com.xueyoucto.xueyou.Example;
import com.xueyoucto.xueyou.Parent;
import com.xueyoucto.xueyou.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public App() {
}
public static void main(String[] args) {
System.out.println("Hello World!");
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Example e = (Example)ctx.getBean("example", Example.class);
System.out.println(e.getAge());
System.out.println(e.getName());
System.out.println("=====================");
Student s = (Student)ctx.getBean("student", Student.class);
System.out.println(s.getAge());
System.out.println(s.getName());
System.out.println("=====================");
Parent p = (Parent)ctx.getBean("parent", Parent.class);
System.out.println(p.getName());
System.out.println(p.getChild().getAge());
System.out.println(p.getChild().getName());
System.out.println("=====================");
Body b = (Body)ctx.getBean("body", Body.class);
System.out.println(b.getName());
System.out.println(b.getLeg().getName());
System.out.println(b.getLeg().getLength());
System.out.println("=====================");
Chinese chinese = (Chinese)ctx.getBean("chinese", Chinese.class);
chinese.test();
}
}
运行结果: