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. Importing Objective-C into Swift

    Overview You can use Objective-C and Swift files together in a single project, no matter which langu ...

  2. vs2019装了WDK后,编译其他vc工程,提示无法打开文件"msvcprtd.lib"

    今天安装了vs2019,而后又安装了wdk,随便写了一个控制台测试程序,居然报错.网上也查了一圈,也没有得到解决.报错内容如下: MSB8038:已启用Spectre缓解,但找不到Spectre缓解库 ...

  3. java线程学习2

    sleep  变为阻塞态  但不释放锁  休眠指定毫秒时间 yield  变为就绪态  可能立即被执行  也可能不立即被执行 join   插队  暂停当前执行的线程  让调用join的线程先执行 线 ...

  4. vue小结1

    (1)渐进式vue 构建用户界面的渐进式框架 只关注视图层 (2)vue中的两个核心点 响应的数据绑定:当数据发生改变时,自动更新视图 利用Object.definedProperty(该属性IE8不 ...

  5. UTF-8,UTF-16

    UTF是 Unicode Translation Format,即把Unicode转做某种格式的意思. 在Unicode基本多文种平面定义的字符(无论是拉丁字母.汉字或其他文字或符号),一律使用2字节 ...

  6. gdb 基础

    版权:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/gdb.html 1. gdb 调试利器 GDB是一个由GNU开源组织发布的.UN ...

  7. NOIP专题复习2 图论-生成树

    目录 一.知识概述 二.典型例题 1.口袋的天空 三.算法分析 (一)Prim算法 (二)Kruskal 四.算法应用 1.[NOIP2013]货车运输 五.算法拓展 1977: [BeiJing20 ...

  8. Python 1-2模块的循环导入问题

    run.py文件: import m1 # 第一次导入 # 验证解决方案一: ''' 正在导入m1 正在导入m2 ''' # print(m1.x) # print(m1.y) # 验证解决方案二: ...

  9. VM搭建hadoop分布式集群

    1.  安装VMware Workstation Pro 2.安装Ubuntu-16.04 3.以下全程使用sudo –s 切换root权限 4.更新deb软件包列表:apt-get update 5 ...

  10. virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenvwrapper.........(解决办法)

    Linux(ubuntu)上python2与python3共存环境下,安装virtualenvwrapper后, 其环境变量被自动设置为VIRTUALENVWRAPPER_PYTHON=/usr/bi ...