给定一个静态XML(如下定义),您可以将spring配置为解组并加载bean而不使用工厂方法。更具体地说,可以以某种方式在应用程序上下文中配置它,以便应用程序自动加载bean。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<outerbean>
<age>20</age>
<innerbean>
<innerName>Inner123</innerName>
<innerAge>10</innerAge>
</innerbean>
</outerbean>
@XmlRootElement(name="outerbean")
@XmlAccessorType(XmlAccessType.FIELD)
@Component
public class OuterBean {
@XmlElement(required=true)
private String name;
@XmlElement(required=true)
private int age;
@XmlElement(name="innerbean")
private InnerBean innerBean;
....
}
@XmlRootElement(name = "innerbean")
@XmlAccessorType(XmlAccessType.FIELD)
public class InnerBean {
@XmlElement(required=true)
private String innerName;
@XmlElement(required=true)
private int innerAge;
....
}