[Java Sprint] Spring XML Configuration : Setter Injection Demo
In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl'
package com.pluralsight.service;
...
public class CustomerServiceImpl implements CustomerService { private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl(); @Override
public List<Customer> findAll() {
return customerRepository.findAll();
}
}
To remove hardcoded Repository, we can use Setter Injection.
First, we defined a setter for 'customerRepository' and remove HibernateCustomerRepositoryImpl():
public class CustomerServiceImpl implements CustomerService {
private CustomerRepository customerRepository;
public void setCustomerRepository(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@Override
public List<Customer> findAll() {
return customerRepository.findAll();
}
}
Second, we setter injection in /java/main/resources/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>
</bean>
</beans>
You can think about each <bean> represent a new Class in Java.
So, first bean:
<bean name="foo" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl"></bean>
reference to HibernateCustomerRepositoryImpl class. Because we want to achieve the same effect:
private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl();
Second bean 'customerService' is actual a setter injection, we want to inject first bean (HibernateCustomerRepositoryImpl) into it and assign to 'customerRepository' property:
<bean name="customerService" class="com.pluralsight.service.CustomerServiceImpl">
<property name="customerRepository" ref="foo"></property>
</bean>
Lastly, we want to use our beans in Application.java:
package com.pluralsight; import com.pluralsight.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main (String[] args) {
// CustomerService service = new CustomerServiceImpl(); // Find the applicationContext.xml file
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// Using application context to replace hardcodedCustomerServiceImpl
CustomerService service = appContext.getBean("customerService", CustomerService.class);
System.out.println(service.findAll().get(0).getFirstname());
}
}
[Java Sprint] Spring XML Configuration : Setter Injection Demo的更多相关文章
- [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 le ...
- [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基础篇——通过Java注解和XML配置装配bean
自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...
- Spring基础篇——通过Java注解和XML配置装配bean(转载)
作者:陈本布衣 出处:http://www.cnblogs.com/chenbenbuyi 本文版权归作者和博客园共有,欢迎转载分享,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留 ...
- Spring Setter Injection and Constructor Injection
Setter Injection AppContext.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- 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 ...
随机推荐
- @import与link方式的区别
1. 老祖宗的差别.link属于XHTML标签,而@import完全是CSS提供的一种方式. link标签除了可以加载CSS外,还可以做很多其它的事情,比如定义RSS,定义rel连接属性等,@impo ...
- 梅沙教育APP简单分析-版本:iOS v1.2.21-Nathaneko-佳钦
梅沙教育APP简单分析 时间:2017年6月6日 版本:iOS v1.2.21 分析人:Nathaneko-佳钦 备注:仅仅是个人一些简单的分析与见解,非正式产品分析报告,未体验购买相关功能,可能存在 ...
- vue+webpack静态资源路径引用
处理静态资产 你可能已经注意到,在项目结构中我们有两个静态资产目录:src/assets和static/.他们之间有什么区别? 要回答这个问题,我们首先需要了解Webpack如何处理静态资产.在*.v ...
- error while loading shared libraries: libclntsh.so.11.1
解决这个问题有两种方法 1.在当前用户下,添加链接库所在路径 LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_ ...
- 德尔福 XE5 安卓调试
https://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp?page=2&am ...
- python 列表生成式、lower()和upper()的使用
参考: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868196389 ...
- Oracle存储过程和程序包
一.为什么要用存储过程? 如果在应用程序中经常需要执行特定的操作,可以基于这些操作简历一个特定的过程.通过使用过程可以简化客户端程序的开发和维护,而且还能提高客户端程序的运行性能. 二.过程的优点? ...
- vue全选与反选以及通过使用如何filter删除数据
在vue学习经常遇到的一些基本问题,下面是购物车里面的部分功能,分享给初学者,直接上源码: <!DOCTYPE html><html> <head> <met ...
- vue 接口统一管理
在外部创建一个API文件夹,然后创建一个API.js 例如 const filmbanner = 'api/billboard/home?__t=1498823077473'; const film ...
- 洛谷——P1602 Sramoc问题
P1602 Sramoc问题 $bfs$搜索 保证第一个搜到的符合条件的就是最小的 #include<bits/stdc++.h> #define N 110000 using names ...