第一种方式
[开发步骤]:
- 为成员变量提供 set get 方法
- 配置文件中进行注入(赋值)
<bean id="userService" class="com.leon.service.UserServiceImpl">
<property name="userDAO">
<bean class="com.leon.dao.UserDAOImpl"/>
</property>
</bean>
第二种方式
第⼀种赋值方式存在的问题:
- 配置文件代码冗余;
- 被注入的对象 (UserDAO)多次创建,浪费(JVM)内存资源。
[开发步骤]:
- 为成员变量提供 set get 方法;
- 配置文件中进行配置;
<bean id="userDAO" class="com.leon.dao.UserDAOImpl"></bean>
<bean id="userService" class="com.leon.service.UserServiceImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
Spring4.x 废除了 <ref local=""/>
基本等效 <ref bean=""/>
;