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 ?
- Creating a new java configuration class.
- Defining a Bean and injecting depedency.
- 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
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.
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 :-
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 :-
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 :-
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.
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.
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.
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.
Leave a Reply