a)通过构造器创建对象
  • 无参构造器. 默认Spring会使用无参构造器创建对象
  • 有参构造器. 可以在下通过标签指定使用有参构造器创建对象. 属性介绍:
  • index: 索引, 从0开始, 表示参数的索引位置
  • name: 名称, 表示参数的名称
  • type: 类型, 表示参数的类型
  • value: 赋值. 当值是简单类型时可以使用. 基本类型, 包装类型, String, resource, class
  • ref: (reference)赋值. 当值为非简单类型时使用, 表示需要引用一个.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Spring管理User对象-->
<bean class="com.bjsxt.pojo.User" id="user">
<constructor-arg name="id" index="0" type="java.lang.Integer" value="123" />
<constructor-arg name="name" index="1" type="java.lang.String" value="张无忌" />
</bean>
</beans>
b)通过工厂创建对象
  • 实例工厂. 需要先创建工厂对象, 然后调用指定的方法创建目标对象`
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--创建工厂对象-->
<bean id="factory" class="com.bjsxt.factory.UserFactory1" />
<!--通过实例工厂创建目标对象-->
<bean id="user" factory-bean="factory" factory-method="getInstance" />
</beans>
  • 静态工厂. 直接通过工厂类调用静态方法就可以创建目标对象
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过静态工厂创建目标对象-->
<bean id="user" class="com.bjsxt.factory.UserFactory2" factory-method="getInstance" />
</beans>

依赖注入的几种方式(对象属性赋值)

a) 构造器注入, 参照IoC.
b) 工厂注入, 参照IoC.
c) 设值注入, 类似于先创建对象, 然后调用setter方法为属性赋值.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="java.util.Date" id="birthday" />
<bean class="com.bjsxt.pojo.Address" id="addr">
<property name="city" value="广州" />
</bean>
<bean class="com.bjsxt.pojo.Card" id="card">
<property name="num" value="187236187236" />
<property name="balance" value="1000" />
</bean>
<bean id="user" class="com.bjsxt.pojo.User">
<!--Properties类型-->
<property name="info">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
<!--Map集合-->
<property name="cardMap">
<map>
<entry key="建行">
<bean class="com.bjsxt.pojo.Card">
<property name="num" value="18273618273" />
<property name="balance" value="200" />
</bean>
</entry>
<entry>
<key>
<value>农行</value>
</key>
<ref bean="card" />
</entry>
</map>
</property>
<!--Set集合-->
<property name="cards">
<set>
<bean class="com.bjsxt.pojo.Card">
<property name="num" value="762318923781" />
<property name="balance">
<value>1</value>
</property>
</bean>
<ref bean="card" />
</set>
</property>
<!--List集合-->
<property name="addrs">
<list>
<bean class="com.bjsxt.pojo.Address">
<property name="city" value="上海" />
</bean>
<ref bean="addr" />
</list>
</property>
<!--自定义对象类型-->
<property name="address">
<bean class="com.bjsxt.pojo.Address">
<property name="city" value="北京" />
</bean>
</property>
<!--数组类型-->
<property name="hob">
<array>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</array>
</property>
<!--Date-->
<!--<property name="birthday" ref="birthday" />-->
<!--<property name="birthday">
<ref bean="birthday" />
</property>-->
<property name="birthday">
<bean class="java.util.Date" />
</property>
<!--String-->
<property name="name" value="张三丰" />
<!--包装类型: Integer-->
<property name="age">
<value>20</value>
</property>
<!--基本数据类型: int-->
<property name="id" value="110" />
</bean>
</beans>