zoukankan      html  css  js  c++  java
  • [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());
        }
    }
  • 相关阅读:
    UML总结4---UML九种图关系说明
    TCP/IP学习笔记__mbuf
    操作系统内存管理之 内部碎片vs外部碎片
    校园招聘面试-操作系统知识总结 分看点三
    操作系统常见面试题总结 分看点二
    操作系统之面试常考 分看点一
    操作系统基础知识总结(二)
    操作系统基础知识总结(一)
    Java HashMap的扩容
    linux查看端口被占用情况
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9472117.html
Copyright © 2011-2022 走看看