Spring:依赖注入的三种方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<!-- spring的依赖注入:
注入的三种方式:
第一种:使用构造函数注入
第二种:使用set注入
第三种:使用注解注入
注入的数据类型有3类:
第一类:基本类型和String类型
第二类:其他bean类型(必须是在spring的配置文件中出现过的bean)
第三类:复杂类型(集合类型)
-->
<!-- 配置资源:把对象的创建交给spring来管理 -->
<!-- 构造函数注入:
涉及的标签:constructor-arg
标签的属性:
type:指定参数的类型
index:指定参数的索引位置,从0开始
name:指定参数的名称 一般用它
================上面三个属性是指定给哪个参数赋值的,下面两个属性是指定赋什么值=================
value:指定基本数据类型或String类型的数据
ref:指定其他bean类型数据
标签出现的位置:
写在bean标签的内部
-->
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
<constructor-arg name="driver" value="com.mysql.jdbc.Driver"></constructor-arg>
<constructor-arg name="port" value="3306"></constructor-arg>
<constructor-arg name="today" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
<!--set方法注入
name:指定参数的set方法名称
================上面三个属性是指定给哪个参数赋值的,下面两个属性是指定赋什么值=================
value:指定基本数据类型或String类型的数据
ref:指定其他bean类型数据
标签出现的位置:
写在bean标签的内部
-->
<bean id="customerService2" class="com.itheima.service.impl.CustomerServiceImpl2">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="port" value="3306"></property>
<property name="today" ref="now"></property>
</bean>
<!-- 复杂类型的注入
结构相同,标签可以互换
-->
<bean id="customerService3" class="com.itheima.service.impl.CustomerServiceImpl3">
<property name="myStrs">
<array>
<value>AAAA</value>
<value>nnnn</value>
</array>
</property>
<property name="myList">
<list>
<value>1111</value>
<value>2222</value>
</list>
</property>
<property name="myMap">
<!-- <map>
<entry key="testD" value="BBB"></entry>
<entry key="testE">
<value>EEE</value>
</entry>
</map>-->
<props>
<prop key="textF">ggg</prop>
<prop key="textK">kkk</prop>
</props>
</property>
<property name="myProps">
<props>
<prop key="textF">ggg</prop>
<prop key="textK">kkk</prop>
</props>
</property>
</bean>
</beans>