版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38409944/article/details/82667912
方法1:外部Bean:ref属性。或者ref标签
<bean id="helloSet" class="hello.Hello">
<property name="name"><value>xx</value></property>
</bean>
<bean id="person" class="hello.Person">
<property name="name"><value>大佬</value></property>
<property name="hello" ref="helloSet"></property>
</bean>
或者
<bean id="person" class="hello.Person">
<property name="name"><value>大佬</value></property>
<property name="hello"><ref bean="helloSet"></ref></property>
</bean>
方法2:内部Bean:不能被外部引用,只能在内部使用。仅仅给一个特定的属性使用,不需要设置任何id或者name
不只是属性注入可以使用内部Bean 构造器注入也可以
<bean id="person" class="hello.Person">
<property name="name"><value>大佬</value></property>
<property name="hello"><bean class="hello.Hello"><property name="name" value="内部Bean"></property></bean></property>
</bean>
方法3:spEL
<bean id="getHello" class="hello.Hello" p:name="你好"></bean>
<bean id="person" class="hello.Person" p:hello="#{getHello}"></bean>