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());
        }
    }
  • 相关阅读:
    两种类型插件系统
    ArcEngine序列化IFeatureRenderer对象
    [WorldWind学习]14.ConfigurationLoader类
    [WorldWind学习]15.模型加载
    AE应用程序开发的思考
    [C++摘抄]void*
    XML文件读写学习
    EA正向工程
    Word2010利用公式编辑器实现公式“显示”“居中”公式编号右对齐
    [Qt]Hello Qt !
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9472117.html
Copyright © 2011-2022 走看看