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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <!-- Define a class, using implementation-->
  7. <bean name="foo" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean>
  8.  
  9. <!-- Setter injection: Inject HibernateCustomerRepositoryImpl to customerRepository -->
  10. <bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl">
  11. <!--<property name="customerRepository" ref="foo"></property>-->
  12. <constructor-arg index="0" ref="foo"></constructor-arg>
  13. </bean>
  14. </beans>
  1. package com.pluralsight.service;
  2.  
  3. import com.pluralsight.model.Customer;
  4. import com.pluralsight.repository.CustomerRepository;
  5.  
  6. import java.util.List;
  7.  
  8. public class CustomerServiceImpl implements CustomerService {
  9.  
  10. //private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl();
  11. private CustomerRepository customerRepository;
  12.  
  13. public CustomerServiceImpl () {
  14.  
  15. }
  16.  
  17. public CustomerServiceImpl (CustomerRepository customerRepository) {
  18. this.customerRepository = customerRepository;
  19. }
  20. /*
  21. public void setCustomerRepository(CustomerRepository customerRepository) {
  22. this.customerRepository = customerRepository;
  23. }
  24. */
  25.  
  26. @Override
  27. public List<Customer> findAll() {
  28. return customerRepository.findAll();
  29. }
  30.  
  31. }

[Java Sprint] Spring XML Configuration : Constructor Injection Demo的更多相关文章

  1. [Java Sprint] Spring XML Configuration : Setter Injection Demo

    In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...

  2. [Java Sprint] Spring Configuration Using Java

    There is no applicationContext.xml file. Too much XML Namespaces helped Enter Java Configuration Cre ...

  3. [Java Spring] Spring Annotation Configuration Using XML

    Add context to our application. main/resources/applicationContext.xml: <?xml version="1.0&qu ...

  4. spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  5. Spring Setter Injection and Constructor Injection

    Setter Injection AppContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...

  6. Spring基础篇——通过Java注解和XML配置装配bean

    自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...

  7. Spring MVC 的 Java Config ( 非 XML ) 配置方式

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml web/pom.xml web.xml WebInitializer.java ...

  8. Spring注解@Configuration和Java Config

    1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...

  9. Spring的@Configuration来代替xml配置

    一. Xml配置法 下面是一个典型的spring配置文件(application-config.xml): [xml] view plain copy <beans> <bean i ...

随机推荐

  1. WOJ600——水题

    第一次做本校OJ的题,被坑的好惨啊! 题目:600.Minimum  Distance 题目大意:给定平面上3个点A.B.C,求平面上的任一顶点P,使得|PA|+2|PB|+3|PC|. 由于刚好在这 ...

  2. eureka 注册中心

    1.eureka版本更新后,pom依赖名称变化 v1.2.7spring-cloud-starter-eureka-server v2.0.0spring-cloud-starter-netflix- ...

  3. 03CSS内容背景

    CSS内容背景 设置背景颜色——background-color  插入背景图片——background-image  设置背景图片位置——background-position 设置重复背景图片—— ...

  4. EasyUI入门,DataGrid(数据表格)

    搭建EasyUI 1.进入官网,下载EasyUI的程序包.地址:http://www.jeasyui.com/download/list.php 2.先导入css样式,引入程序包 3.进入EasyUI ...

  5. LAME的“命令行”

    VBR 编码 (强烈推荐) Alt Preset Extreme (平均256kbps) 我们有时在网上可以看到".LAME-APX." 就是这种形式,我们也可以在文件名中包含这个 ...

  6. Web项目ConcurrentModificationException异常

    后台SSH在做Session删除的时候,遇到了ConcurrentModificationException异常. 参考资料:http://blog.csdn.net/idesvo/article/d ...

  7. linux core dump 生成和调试

    core dump 某些信号的产生会导致产生core dump,包含了进程终止时的内存镜像.在某些时候这个core文件就非常的有用处,配合gdb或者lldb调试起来非常方便. 更详细的文档参考 Lin ...

  8. 使用Hashids来保护你的数据库主键

    为什么要保护数据库主键? 数据库主键一般是有序自增主键,极易被爬虫抓取数据,作为应用开发者,这是不应该的,你辛辛苦苦收集的数据转眼之间被其他人给抓取了,是不是很大的损失? Hashids的介绍 gen ...

  9. mysql 删除数据重复的记录

    delete from user where id not in ( select * from ( select min(id) from user group by username,email ...

  10. python 列表(二)

     列表的其他操作 count 用于统计列表中某个元素出现的次数 Eg: extend 把一个列表添加到另一个列表里面 Index 输出元素的位置即根据内容索引位置 Reverse 把列表元素的位置倒过 ...