spring automatically call the set method

applicationContext.xml added the name attribute and bean1

<bean name="bean1" class="bean.bean1"></bean>
    <bean name="bean2" class="bean.bean2">
    <property name="bean1" ref="bean1"></property>
    <property name="name" value="test"></property>
    </bean>

 

The method set output statement added bean2.java

public void setBean1(bean1 bean1){
    System.out.println("bean2注入bean1:"+bean1);
    this.name=bean1.getName();
}
public void setName(String name) {
    this.name = name;
    System.out.println("bean2修改name:"+name);
}

 

TempTest.java added

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

 

After the first run will output

bean2 injection Bean1: Bean1 [name = 123123]
bean2 Review name: test

 

Guess you like

Origin www.cnblogs.com/withbear/p/11839987.html