public class Person{ public Person(){ System.out.println("Person person person ..."); } } public class Leader { public Leader(){ System.out.println("leader leader leader ..."); } } public class Group { public Group(){ System.out.println("Group group..."); } } public class Main { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("a.xml"); } } <bean id="p" class="com.Person" /> <bean id="leader" class="com.Leader" depends-on="p"/> <bean id="grouop" class="com.Group" depends-on="leader"/>
打印:
Person person person ...
leader leader leader ...
Group group...
使用depends-on可以设置bean依赖,只有depends-on列表中的bean才会被创建。