Spring Configuration with Java code ( No XML )

In this article, we will learn how to do spring Configuration with Java code. We will also see a sample example which will help us to understand this topic more clearly.

We will use the java source code to do the spring configuration instead of XML.

Ways to do spring configuration

Full XML configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Define your beans here -->    
    <bean id="address" class="com.getinputs.AddressImpl"></bean>
    
    <bean id="employee" class="com.getinputs.Manager">
       <property name="employeeAddress" ref="address"></property>
    </bean>   
</beans>

You can learn about full XML configuration here https://getinputs.com/spring-dependency-injection/

XML Component scan

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.getinputs" />      
</beans>

You can learn about XML component scan here https://getinputs.com/dependency-injection-with-annotation/

Java Configuration class

Here we will not be using any XML and we will be using pure java code to configure the container.

We have to create a configuration class. We have to annotate the configuration class with @Configuration annotation. Spring container will look for a class that is annotated with @Configuration annotation and create the spring beans for the classes made available in the Configuration class.

We also have to enable the Component scan feature on the Configuration class. This feature tells the container to scan the classes in the base package to create the spring beans.

package com.getinputs;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.getinputs")
public class BeanConfiguration {
}

We will create a Manager class inside the base package and annotate the class with @Component annotation. @Component annotation marks the beans as spring-managed components.

package com.getinputs;

import org.springframework.stereotype.Component;

@Component
public class Manager {

  String name;
  int salary;

  public String getName() {
    return "Suraj";
  }

  public int getSalary() {
    return 50000;
  }
}

Now we will create an Annotation config application context with the Configuration class as a parameter. Once the context gets created then we can get the Manager bean and we can call its methods.

package com.getinputs;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringDemoApp {
  public static void main(String[] args) {

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);

    Manager manager = context.getBean("manager", Manager.class);
    System.out.println("Manager's name is : " + manager.getName());
    System.out.println("Manager's salary is : " + manager.getSalary());

    context.close();
  }
}

Output:
Manager 's name is : Suraj
Manager 's salary is : 50000

As we are getting a proper output so it means we are able to configure the Manager bean using spring configuration with pure java code.

https://github.com/getinputs/samples/tree/main/java-configuration-scan

The above is a very simple approach for Spring Configuration with Java code.

In a few cases, we will not be able to use the above component scan approach. So we have to use a different approach.

We have to create a configuration class but this class will not use @ComponentScan annotation instead we will create the beans manually and annotate the beans with @Bean annotation.

We use the @Bean annotation on the method which specifies that this method will return a bean that spring context will manage.

@Bean("manager")
public Manager getManagerInstance() {
  return new Manager();
}

In the case of the component-scan feature to do the dependency injection, we can easily use the @Autowired annotation. But when we are using the @Bean approach how we can do that?

We will understand this better with an example. Say we are having a Manager and an Address bean. We have to inject the Address bean in the Manager using constructor injection. Firstly we will create an Address class.

package com.getinputs;

public class Address {

  String address;

  public String getAddress() {
    return "Civil Lines, Delhi";
  }
}

Now we will create a Manager class that will take Address bean via the constructor.

package com.getinputs;

public class Manager {

  String name;
  int salary;
  Address address;

  Manager(Address address) {
    this.address = address;
  }

  public String getName() {
    return "Suraj";
  }

  public int getSalary() {
    return 50000;
  }

  public String getAddress() {
    return address.getAddress();
  }
}

Now we will create the configuration class like below

package com.getinputs;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfiguration {

  @Bean
  public Address getAddressInstance() {
    return new Address();
  }

  @Bean("manager")
  public Manager getManagerInstance() {
    return new Manager(getAddressInstance());
  }
}

Here we are creating the Address instance and passing the address instance to the Manager instance via a constructor. Also, we are annotating both methods with @Bean annotation to specify these beans will be managed by the spring container.

Now we will create an Annotation config application context with the Configuration class as a parameter. Once the context gets created then we can get the Manager bean and we can call its methods.

package com.getinputs;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringDemoApp {
  public static void main(String[] args) {

    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);

    Manager manager = context.getBean("manager", Manager.class);
    System.out.println("Manager's name is : " + manager.getName());
    System.out.println("Manager's salary is : " + manager.getSalary());
    System.out.println("Manager's address is : " + manager.getAddress());

    context.close();
  }
}

Output:
Manager 's name is : Suraj
Manager 's salary is : 50000
Manager 's address is : Civil Lines, Delhi

As we are getting a proper output so it means we are able to configure the Manager bean and we are able to do the constructor dependency injection using spring configuration with pure java code.

https://github.com/getinputs/samples/tree/main/java-configuration-beans

So this is all about how to do spring configuration with Java code. I hope you found this article interesting and valuable. If you are having any concerns or questions about this article please comment below and please share it to help me grow.

Leave a Comment