属性注入property
构造方法注入constructor-arg
没有使用构造方法注入的,对应的bean必须有无参的构造方法
有构造方法注入的,进入有参构造函数,若有属性注入,再执行set方法注入。
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld h=(HelloWorld)(ctx.getBean("helloWorld"));
h.hello();
配置文件的几种形式 及其结果 :
<bean name="helloWorld" class="com.asi.spring.beans.HelloWorld">
<property name="user" value="1"></property>
<constructor-arg value="Eric"></constructor-arg>
</bean>
后台输出
setUser:1
Hello: 1
=====================================
<bean name="helloWorld" class="com.asi.spring.beans.HelloWorld">
<property name="user" value="1"></property>
</bean>
HelloWorld’s constructor…
setUser:1
Hello: 1
=====================================
<bean name="helloWorld" class="com.asi.spring.beans.HelloWorld">
<constructor-arg value="Eric"></constructor-arg>
</bean>
Hello: Eric
=====================================