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. codeforces_1065_D.three pieces_思维

    题意:一个正方形棋盘,三种棋子,knight:像中国象棋中的马一样走:bishop:斜着走:rook:中国象棋中的车.棋盘中每个格子中标着1--n*n的互不相同的数字,从1开始任选一种棋子开始走,在每 ...

  2. sh NonUniqueObjectException

    话题引入: 使用hibernate进行更新操作时,首先调用了findById方法获取要修改的对象,此时session没有被关闭,接着重新创建一个对象,将要修改的属性值赋值给这个对象.调用修改方法抛出如 ...

  3. vue+webpack静态资源路径引用

    处理静态资产 你可能已经注意到,在项目结构中我们有两个静态资产目录:src/assets和static/.他们之间有什么区别? 要回答这个问题,我们首先需要了解Webpack如何处理静态资产.在*.v ...

  4. idea文件全部变红, 文件全部红色

    idea如果当前project用了版本控制器,其下面新建的所有的项目默认都是加入到版本控制里面,所以项目名称和文件都是红色的,如图: 看起来非常不爽, 那么如何解决呢? File–>Settin ...

  5. 利用jQuery对li标签操作

    <ul class="con" id="products"> <li i=" class=""> < ...

  6. pytorch记录:seq2seq例子看看这torch怎么玩的

    先看看简单例子: import torch import torch.autograd as autograd import torch.nn as nn import torch.nn.functi ...

  7. extjs中Store和grid的刷新问题

    问题1:Store.load() 和Store.setproxy()区别 问题2:修改后的Grid 更新: Store.reload() 问题3,store删除后刷新会出问题 Store移除一行:St ...

  8. vue开发调试工具vue-devtools安装

    vue开发调试工具别人总结的非常好,所以直接把链接拿过来了,就当做个笔记了,也希望能帮到有需要的人,感谢“沉着前进”,来源(https://www.cnblogs.com/fighxp/p/78150 ...

  9. 笔试算法题(53):四种基本排序方法的性能特征(Selection,Insertion,Bubble,Shell)

    四种基本算法概述: 基本排序:选择,插入,冒泡,希尔.上述算法适用于小规模文件和特殊文件的排序,并不适合大规模随机排序的文件.前三种算法的执行时间与N2成正比,希尔算法的执行时间与N3/2(或更快)成 ...

  10. Python 迭代器-生成器-面向过程编程

    上节课复习:1. 函数的递归调用 在调用一个函数的过程中又直接或者间接地调用了函数本身称之为函数的递归 函数的递归调用有两个明确的阶段: 1. 回溯 一层一层地调用本身 注意: 1.每一次调用问题的规 ...