spring的三种注入方式(伪代码)
(需set方法)
name:用于指定注入时所调用的set方法名称
value:用于提供基本类型和string类型的数据
ref:用于指定其他bean类型的数据(需自定义)
<bean id=" " class=" ">
<property name="set方法名" value="赋值"></property>
自定义bean例举:(此时value处应使用ref="now")
<bean id="now" class="java.util.Data"></bean>
构造函数注入格式:
(需构造函数,参数不可缺,否则注入无效)
name:用于指定注入时所调用的set方法名称
value:用于提供基本类型和string类型的数据
ref:用于指定其他bean类型的数据(需自定义)
<bean id=" " class=" ">
<constructor-arg name="名" value="赋值"></constructor-age>
</bean>
(剩余同上)
复杂类型注入/集合注入格式:
(基于set方法之上,新增其他变量类型,新增set)
用于给List结构注入:list array set
用于给Map结构注入:map props
结构相同,标签可以互换
<bean id=" " class=" ">
<property name="set方法名">
<array>(可换list set)
<value>AAA</value>
</array>
</property>
<property name="set方法名">
<map>(两种方法)
</entry>
<entry key=" ">
<value>BBB</value>
</entry>
</map></property>
<property name="set方法名">
<props>
<prop key=" ">CCC</prop>
</props>
</property>