spring bean的构造器注入

来源:互联网 发布:win10网络图标不能启用 编辑:程序博客网 时间:2024/05/20 07:32
public class Juggler implements Performer{private int beanBags=3;private Poem poem;public Juggler(){}public Juggler(int beanBags,Poem poem){this.beanBags = beanBags;this.poem = poem;}public void perform() throws Exception{System.out.println("Juggler..........."+beanBags+"............going");System.out.println(poem.getId()+".............."+poem.getName());}}

public class Poem {private String id;private String name;public Poem(String id,String name){this.id = id;this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}


测试类

public class Test {public static void main(String[] args) throws Exception {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Juggler juggler = (Juggler) context.getBean("dudk");juggler.perform();}}

0 0