[Java Sprint] Spring XML Configuration : Constructor Injection Demo
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的更多相关文章
- [Java Sprint] Spring XML Configuration : Setter Injection Demo
In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...
- [Java Sprint] Spring Configuration Using Java
There is no applicationContext.xml file. Too much XML Namespaces helped Enter Java Configuration Cre ...
- [Java Spring] Spring Annotation Configuration Using XML
Add context to our application. main/resources/applicationContext.xml: <?xml version="1.0&qu ...
- spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入
一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...
- Spring Setter Injection and Constructor Injection
Setter Injection AppContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- Spring基础篇——通过Java注解和XML配置装配bean
自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...
- Spring MVC 的 Java Config ( 非 XML ) 配置方式
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml web/pom.xml web.xml WebInitializer.java ...
- Spring注解@Configuration和Java Config
1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...
- Spring的@Configuration来代替xml配置
一. Xml配置法 下面是一个典型的spring配置文件(application-config.xml): [xml] view plain copy <beans> <bean i ...
随机推荐
- 洛谷 P1615 西游记公司
题目背景 一道极其无厘头的题目 题目描述 事情是这样的:西游记中的孙沙猪(孙杀猪)三徒弟在西天取经之后开始进入厦门大学经贸系学习经济,在1个小时的学习后,他们用暴力手段毕业了.然后,他们创办了三个公司 ...
- org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
---恢复内容开始--- /* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache ...
- (转)淘淘商城系列——分布式文件系统FastDFS
http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...
- codeforces_B. Forgery
http://codeforces.com/contest/1059/problem/B 题意: For simplicity, the signature is represented as an ...
- js 零散知识
# 同一种类型的事件注册多个事件句柄,后面的不会覆盖前面的事件 # event.which == 13,13代表回车 # parsley.js验证框架 # JSON.stringify, avoid ...
- vue 添加 fastclick的支持
fastclick:处理移动端click事件300毫秒延迟 1.兼容性iOS 3及更高版本的移动SafariiOS 5及更高版本的ChromeAndroid上的Chrome(ICS)Opera Mob ...
- RabbitMQ 基础概念介绍
AMQP 消息模型 RabbitMQ 是基于 AMQP(高级消息队列协议)的一个开源实现,其内部实际也是 AMQP 的基本概念.
- pom.xml配置引用项目时不生效
1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.
- 快速创建你xmlhttp的方法
function initxmlhttp() { var xmlhttp try { xmlhttp=new ActiveXObject("Msxml2.XM ...
- 如何判断页面是在移动端还是PC端打开的呢
1. window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) ? " ...