Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html

Now let's see how to cover setter injection to coustructor injection. Notice, don't need to compare which one is better, you can use both.

Different from setter injection which use 'name', constructor injection using 'index'.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"> <!-- Define a class, using implementation-->
<bean name="foo" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean> <!-- Setter injection: Inject HibernateCustomerRepositoryImpl to customerRepository -->
<bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl">
<!--<property name="customerRepository" ref="foo"></property>-->
<constructor-arg index="0" ref="foo"></constructor-arg>
</bean>
</beans>
package com.pluralsight.service;

import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository; import java.util.List; public class CustomerServiceImpl implements CustomerService { //private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl();
private CustomerRepository customerRepository; public CustomerServiceImpl () { } public CustomerServiceImpl (CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
/*
public void setCustomerRepository(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
*/ @Override
public List<Customer> findAll() {
return customerRepository.findAll();
} }

最新文章

  1. 贪心算法-最小生成树Kruskal算法和Prim算法
  2. BZOJ_1606_ [Usaco2008_Dec]_Hay_For_Sale _购买干草_(背包)
  3. centos防火墙操作
  4. mvc在页面上显示PDF
  5. 第06讲- DDMS中logcat的使用
  6. Criteria 和 DetachedCriteria的区别与使用(转)
  7. 判断字符串中是否包含指定的内容&amp;&amp;字符串截取方法比较说明
  8. golang其实也可以优先调度
  9. sizeof与strlen的不同
  10. maven创建web工程
  11. 程序员如何让自己 Be Cloud Native - 配置篇
  12. 基于Clang的缓存型C++编译器Zapcc
  13. 【2019年04月09日】A股净资产收益率ROE最高排名
  14. swoole web服务
  15. Django框架---- 自定义分页组件
  16. kafka 知识点
  17. 使用adb命令对手机进行截屏保存到电脑,SDCard
  18. iOS下载图片失败
  19. Python开发基础-Day9-生成器、三元表达式、列表生成式、生成器表达式
  20. 算法训练 Balloons in a Box (枚举,模拟)

热门文章

  1. 使用迅为iTOP-iMX6开发板-uboot-修改默认环境变量
  2. CREATE GROUP - 定义一个新的用户组
  3. Java基础(十三)--深拷贝和浅拷贝
  4. 二维码之zxing仿新浪微博二维码
  5. mysql 使用ip地址连接不上;MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql
  6. php基础排序算法
  7. python 深复制和浅复制
  8. sqllite相关总结
  9. react-native IOS Build input file cannot be found: &#39;~~~~~/node_modules/react-native/Libraries/WebSocket/libfishhook.a&#39;
  10. CCF计算机职业资格认证考试 201809-2 买菜