CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   JAVA   Spring Configuration Class in JAVA [Class VS XML Config]

Spring Configuration Class in JAVA [Class VS XML Config]

September 16, 2022 by SNK

Spring Java Based Configurations

Today we are going to learn, how to configure spring framework using java class. Last time we wrote the article about spring configuration using xml which can be found by clicking this link. If you want to go through it first, it would really build up the base.

We also took a quick look at spring configuration with java annotation which reduces a lot of code inside the xml file but still xml configuration was needed. But, now we will configure it completely with Java class and there would not be any need of creating the xml file at all. This will reduce a lot of verbose code and helps in clean definition of everything.

Let’s get started

What are we going to do ?

  1. Creating a new java configuration class.
  2. Defining a Bean and injecting depedency.
  3. Injecting values from properties file.

1. Creating a new Java Configuration Class

Just like creating the applicationContext.xml file inside /resources folder, we need to create a new java configuration class named ApplicationConfig.java . Then, we will annotate it with @Configuration annotation to let know spring about our configuration class. We will also set @ComponentScan("com.learning.spring") which is the base package of our project.

Doing this, spring framework will scan all the files inside the package and register the class as beans which has been added into our configuration class. Let’s take a look at example.

Java Configuration Class

ApplicationConfig.java
Java
1
2
3
4
5
@Configuration
@ComponentScan("com.learning.spring")
public class ApplicationConfig {
   ...
}

So, now you have registered the java configuration class, now we can define@Bean inside it. But, first let’s see the xml configuration equivalent of this code.

applicationContext.xml
XHTML
1
2
3
4
5
6
7
8
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
 
    <context:component-scan base-package="com.learning.spring" />
</beans>

2. Defining a Bean and Injecting Dependency

Once, you have created a configuration class, you can now add your @Beans to it and add it to your configuration class and you can use it via your spring application context.

Take a look at the example below :-

ApplicationConfig.java
Java
1
2
3
4
5
6
7
8
@Configuration
@ComponentScan("com.learning.spring")
public class ApplicationConfig {
    @Bean
    public Dog theDog() {
        return new Dog();
    }
}

So, above you can see, I have registered a @Bean of the class Dog.java, where your method name which is theDog will be reference you are going to use when pulling it from the your application context.

Before we used to use ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); but now you have to use AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);  when we are working with annotation configurations. Take a look at the example below :-

Dog.java
Java
1
2
3
4
5
6
7
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        Dog theDog = context.getBean("theDog", Dog.class);
        theDog.animalSpeaks();
    }
}

Injecting Dependency

In the above steps we have created a Bean, but we often require to inject service as our dependency. Spring has made it very easy to inject depedency directly from our spring configuration class. Take a  look below :-

ApplicationConfig.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
@ComponentScan("com.learning.spring")
public class ApplicationConfig {
 
    @Bean
    public AnimalService theAnimalService() {
        return new LandAnimalService();
    }
 
    @Bean
    public Dog theDog() {
        return new Dog(theAnimalService());
    }
}

Make sure to add it in your constructor as well.

Dog.java
Java
1
2
3
4
5
private AnimalService animalService;
 
public Dog(AnimalService theAnimalService) {
    animalService = theAnimalService;
}

3. Injecting Values from Properties File

Sometimes values needs to initialize at the time runtime to use in our apps. Let’s say like the environment variable that we might need to use to configure our production or tests environment. In that case, it becomes extremly useful in such scenarios.

However its very easy to inject the values from properties file directly into our java class. We just have to add @PropertySource annotation in our Java Configuration class.

ApplicationConfig.java
Java
1
2
3
4
5
6
@Configuration
@ComponentScan("com.learning.spring")
@PropertySource("classpath:application.properties")
public class ApplicationConfig {
    ...
}

Next step is to add @Value annotation to our property and use it in your setter methods.

Dog.java
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.learning.spring;
 
import org.springframework.beans.factory.annotation.Value;
 
public class Dog implements Animal {
 
    @Value("${animal.name}")
    public String animalName;
    
    public void getAnimalName() {
       System.out.println(this.animalName);
    }
}

Feel free to comment and start the discussion.

SHARE ON
Buffer

Enjoyed this article?

Like us on

JAVA

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.