Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Spring Top – Temp – Non-Geo (Lightrun)

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

>> LEARN SPRING
NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

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.1.4.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.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> THE COURSE
Generic footer banner
Comments are closed on this article!