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的更多相关文章

  1. [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 ...

  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基础篇——通过Java注解和XML配置装配bean

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

  5. Spring基础篇——通过Java注解和XML配置装配bean(转载)

      作者:陈本布衣 出处:http://www.cnblogs.com/chenbenbuyi 本文版权归作者和博客园共有,欢迎转载分享,但必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留 ...

  6. Spring Setter Injection and Constructor Injection

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

  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. vue2.0 路由传参(router-link传过去)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 迅为4412嵌入式安卓开发板兼容3G网络|4G网络

    iTOP-Exynos4412开发板内置有无线 WIFI 模块.Bluetooth.GPS.Camera.3G等模组,陀螺仪等,支持 HDMI1.4(1080P/60Hz)显示,客户可以直接从开发平台 ...

  3. cesium primitive方式 ————http://blog.sina.com.cn/s/blog_15e866bbe0102y0ji.html

    Cesium学习笔记-工具篇17-PrimitivePoint自定义渲染-点 (2018-08-28 16:12:06) 转载▼ 标签: cesium primitive 自定义渲染 shader c ...

  4. 正则表达式,匹配查找函数(preg_match_all)flags参数对比

    格式: int preg_match_all ( string pattern, string subject, array matches [, int flags] ) 参数 flags 选项有以 ...

  5. Less用法注意事项

    (1)引入顺序 引入你的 .less 样式文件的时候要设置 rel 属性值为 “stylesheet/less”: <link rel="stylesheet/less" t ...

  6. git 提交运用vim编辑器

    git  commit -m 默认使用nano,觉得不爽,改成vim吧.在 .gitconfig (在根目录下)的  [core] 段中加上 editor=vim . 或:$git config -- ...

  7. Python 1-3区分Python文件的两种用途和模块的搜索路径

    区分Python文件的两种用途 run.py文件: import m1 m1.py文件: def f1(): print('f1') def f2(): print('f2') #当文件被执行时__n ...

  8. nginx配置location项的URL匹配规则

    Localtion URL的正则匹配规则 示例 location / { try_files $uri @apache; } #所有的路径都是/开头,表示匹配所有 location @apache { ...

  9. FFmpeg加水印

    ffmpeg中文水印乱码两种原因 1.字符编码格式原因,中文必须是utf8编码格式的(我遇到的问题,在vs2013上写的中文,已做编码格式转码,放到centos7.2上编译运行也会出现中文乱码的问题, ...

  10. <input type="button" /> 和<input type="submit" /> 的区别

    <input type="button" /> 这就是一个按钮.如果你不写javascript 的话,按下去什么也不会发生.<input type="s ...