Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3522

XML-Based Injection in Spring

$
0
0

1. Introduction

In this basic tutorial, we’ll learn how to do simple XML-based bean configuration with the Spring Framework.

2. Overview

Let’s start by adding Spring’s library dependency in the pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.1.RELEASE</version>         
</dependency>

The latest version of the Spring dependency can be found here.

3. Dependency Injection – An Overview

Dependency injection is a technique whereby dependencies of an object are supplied by external containers.

Let’s say we’ve got an application class that depends on a service that actually handles the business logic:

public class IndexApp {
    private IService service;
    // standard constructors/getters/setters
}

Now let’s say IService is an Interface:

public interface IService {
    public String serve();
}

This interface can have multiple implementations.

Let’s have a quick look at one potential implementation:

public class IndexService implements IService {
    @Override
    public String serve() {
        return "Hello World";
    }
}

Here, IndexApp is a high-level component that depends on the low-level component called IService.

In essence, we’re decoupling IndexApp from a particular implementation of the IService which can vary based on the various factors.

4. Dependency Injection – In Action

Let’s see how we can inject a dependency.

4.1. Using Properties

Let’s see how we can wire the dependencies together using an XML-based configuration:

<bean 
  id="indexService" 
  class="com.baeldung.di.spring.IndexService" />
     
<bean 
  id="indexApp" 
  class="com.baeldung.di.spring.IndexApp" >
    <property name="service" ref="indexService" />
</bean>    

As can be seen, we’re creating an instance of IndexService and assigning it an id. By default, the bean is a singleton. Also, we’re creating an instance of IndexApp.

Within this bean, we’re injecting the other bean using setter method.

4.2. Using Constructor

Instead of injecting a bean via the setter method, we can inject the dependency using the constructor:

<bean 
  id="indexApp" 
  class="com.baeldung.di.spring.IndexApp">
    <constructor-arg ref="indexService" />
</bean>    

4.3. Using Static Factory

We can also inject a bean returned by a factory. Let’s create a simple factory that returns an instance of IService based on the number supplied:

public class StaticServiceFactory {
    public static IService getNumber(int number) {
        // ...
    }
}

Now let’s see how we could use above implementation to inject a bean into IndexApp using an XML-based configuration:

<bean id="messageService"
  class="com.baeldung.di.spring.StaticServiceFactory"
  factory-method="getService">
    <constructor-arg value="1" />
</bean>   
  
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
    <property name="service" ref="messageService" />
</bean>

In the above example, we’re calling the static getService method using factory-method to create a bean with id messageService which we inject into IndexApp.

4.4. Using Factory Method

Let’s consider an instance factory that returns an instance of IService based on the number supplied. This time, the method is not static:

public class InstanceServiceFactory {
    public IService getNumber(int number) {
        // ...
    }
}

Now let’s see how we could use above implementation to inject a bean into IndexApp using XML configuration:

<bean id="indexServiceFactory" 
  class="com.baeldung.di.spring.InstanceServiceFactory" />
<bean id="messageService"
  class="com.baeldung.di.spring.InstanceServiceFactory"
  factory-method="getService" factory-bean="indexServiceFactory">
    <constructor-arg value="1" />
</bean>  
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
    <property name="service" ref="messageService" />
</bean>

In the above example, we’re calling the getService method on an instance of InstanceServiceFactory using factory-method to create a bean with id messageService which we inject in IndexApp.

5. Testing

This is how we can access configured beans:

@Test
public void whenGetBeans_returnsBean() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("...");
    IndexApp indexApp = applicationContext.getBean("indexApp", IndexApp.class);
    assertNotNull(indexApp);
}

6. Conclusion

In this quick tutorial, we illustrated examples of how we can inject dependency using the XML-based configuration using Spring Framework.

The implementation of these examples can be found in the GitHub project – this is a Maven-based project, so it should be easy to import and run as is.


Viewing all articles
Browse latest Browse all 3522

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>