Spring DI(依赖注入)
作者:互联网
DI(依赖注入)Dependency Injection
1 DI是什么,DI可以理解为IOC的一部分
2 举例:当一个类A中需要依赖另一个类B对象时,把B赋值给A的过程就叫做依赖注入
代码示例:
<bean id="peo2" class="com.qyx.Bean.PeopleFactory" factory-method="newInstance02"/> <bean id="peo3" class="com.qyx.Bean.People"> <!-- 下面赋值方法等效于 <property name="id"> <value>100</value> </property> --> <property name="id" value="10"></property> <property name="name" value="Test"></property> <property name="job" value="CTR"></property> <property name="major" value="QWE"></property> <property name="sets"> <set> <value>aaaaa</value> <value>bbbbb</value> </set> </property> <!--List赋值的另一种写法<property name="list" value="1,2,3"> 如果list中只有一个值 --> <property name="lists"> <list> <value>sdsa</value> </list> </property> <!--数组赋值的另一种写法<property name="strs" value="1,2,3"> 如果数组中只有一个值 --> <property name="strs"> <array> <value>1</value> <value>2</value> <value>3</value> </array> </property> <property name="maps"> <map> <entry key="a" value="b"/> </map> </property> <!--为properties属性赋值--> <!--<property name="demo"> <props> <prop key="aa">bb</prop> </props> </property>--> <property name="desk" ref="desk"></property> </bean> <bean id="desk" class="com.qyx.Bean.Desk"> <property name="id" value="1"></property> <property name="price" value="12"></property> </bean> <bean id="desk2" class="com.qyx.Bean.Desk" scope="singleton"> <constructor-arg name="id" value="10"></constructor-arg> <constructor-arg name="price" value="20"></constructor-arg> </bean> <bean id="desk3" class="com.qyx.Bean.Desk" scope="prototype"> <constructor-arg name="id" value="20"></constructor-arg> <constructor-arg name="price" value="60"></constructor-arg> </bean> <bean class="com.qyx.Bean.InitHelloWorld"></bean> </beans>
<bean id="peo2" class="com.qyx.Bean.PeopleFactory" factory-method="newInstance02"/>
<bean id="peo3" class="com.qyx.Bean.People">
<!--
下面赋值方法等效于
<property name="id">
<value>100</value>
</property>
-->
<property name="id" value="10"></property>
<property name="name" value="Test"></property>
<property name="job" value="CTR"></property>
<property name="major" value="QWE"></property>
<property name="sets">
<set>
<value>aaaaa</value>
<value>bbbbb</value>
</set>
</property>
<!--List赋值的另一种写法<property name="list" value="1,2,3">
如果list中只有一个值
-->
<property name="lists">
<list>
<value>sdsa</value>
</list>
</property>
<!--数组赋值的另一种写法<property name="strs" value="1,2,3">
如果数组中只有一个值
-->
<property name="strs">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<property name="maps">
<map>
<entry key="a" value="b"/>
</map>
</property>
<!--为properties属性赋值-->
<!--<property name="demo">
<props>
<prop key="aa">bb</prop>
</props>
</property>-->
<property name="desk" ref="desk"></property>
</bean>
<bean id="desk" class="com.qyx.Bean.Desk">
<property name="id" value="1"></property>
<property name="price" value="12"></property>
</bean>
<bean id="desk2" class="com.qyx.Bean.Desk" scope="singleton">
<constructor-arg name="id" value="10"></constructor-arg>
<constructor-arg name="price" value="20"></constructor-arg>
</bean>
<bean id="desk3" class="com.qyx.Bean.Desk" scope="prototype">
<constructor-arg name="id" value="20"></constructor-arg>
<constructor-arg name="price" value="60"></constructor-arg>
</bean>
<bean class="com.qyx.Bean.InitHelloWorld"></bean>
</beans>
标签:依赖,bb,DI,Spring,sdsa,aaaaa,bbbbb 来源: https://www.cnblogs.com/qyx66/p/11960194.html