[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());
    }
}

 

posted @ 2018-08-14 02:26  Zhentiw  阅读(275)  评论(0编辑  收藏  举报