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();
} }

[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. 合并百度影音的离线数据 with python 第二版 基于yield

    重新整理了一下代码. 增加了bdv,mkv的处理流程. 目前暂时支持windows平台. bdv,mkv,rmvb的不同处理流程 # -*- coding: UTF-8 -*- import os i ...

  2. 语音行业技术领先者Nuance上海诚招ASR/NLP研发工程师和软件工程师

    Nuance is a leading provider of voice and language solutions for businesses and consumers around the ...

  3. [转帖]4412开发板/4418开发板Android4.4.4实现ble功能

    本文转自迅为论坛:http://bbs.topeetboard.com ①.4418开发板实现ble功能方法: 在4418/android/device/nexell/drone2/device.mk ...

  4. (转)Spring中的事务操作

    http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...

  5. 深入理解python对象及属性

    类属性和实例属性首先来看看类属性和类实例的属性在python中如何存储,通过__dir__方法来查看对象的属性 >>> class Test(object): pass>> ...

  6. Python 源码分析:queue 队列模块

    起步 queue 模块提供适用于多线程编程的先进先出(FIFO)数据结构.因为它是线程安全的,所以多个线程很轻松地使用同一个实例. 源码分析 先从初始化的函数来看: 从这初始化函数能得到哪些信息呢?首 ...

  7. createlang - 定义一种新的 PostgreSQL 过程语言

    SYNOPSIS createlang [ connection-option...] langname [ dbname] createlang [ connection-option...] -- ...

  8. 流行-Manifold【0】-维基百科中文版本解释

  9. 笔试算法题(08):输出倒数第K个节点

    出题:输入一个单向链表,要求输出链表中倒数第K个节点 分析:利用等差指针,指针A先行K步,然后指针B从链表头与A同步前进,当A到达链表尾时B指向的节点就是倒数第K个节点: 解题: struct Nod ...

  10. hdu 1787

    #include<stdio.h> #include<math.h> int huzhi(int n) {//欧拉函数公式 int i,j=n; for(i=2;i<(i ...