Pure Java-based Configuration in Spring.

hemasai jammana
3 min readApr 8, 2023

--

We have seen different ways of configuring the spring container in the earlier articles. Now we will see the spring application using pure Java code.

Spring

To perform dependency injection, we must provide configuration metadata to the spring IOC container.
There are 4 different ways of configuring metadata
1. XML-based approach.
2. Annotation-driven configuration.
3. Using Java code configuration.
4. Spring boot AutoDriven configuration.

Out of these, we have already seen XML-based approach and Annotation-driven configuration, now we will see the Java code configuration.
In the XML-based and Annotation-driven configurations, we have used two containers Bean Factory and Application Context to get the beans.
Now to work on Java-based configuration Bean factory won’t work we should only work on Application Context.

Application Context Container:
1.
This is an extension of the bean factory.
2. Implementation classes of ApplicationContext(I) are below:
a. FileSystemXmlApplicationContext(standalone)
b. ClassPathXmlApplicationContext(standalone)
c. XmlWebApplicationContext(SpringMVC apps)
d. AnnotationConfigApplicationContext(Standaloneapp’s)
e. AnnotationConfigWebApplicationContext(SpringMVC apps)

We will use AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext based on the requirement when we are working using Java-based configuration.

In a pure Java-based configuration Spring application, the application context is configured entirely using Java code, rather than XML configuration files. This approach provides a more flexible and type-safe way to configure the application context and can make it easier to maintain and refactor the application.

Steps / Rules to build Java-based projects easily:
1. Annotate all the user-defined classes using stereotype annotations(@Component, @Service, @Repository) so the spring container can manage beans. (or)
we can also use the @Bean annotation in the configuration class to generate beans of the required class.

2. Add one configuration class and annotate with @Configuration and we can use @ComponentScan to scan all the beans in the provided base package.

Note :- Configuration internally creates a component so the container can access this class and this class will become the configuration class for the application.

@Configuration
@ComponentScan(basePackages = "src")
public class AppConfig {

.....//If any pre defined class need to provide beans information here.
}

3. Configure the pre-defined classes using @Bean in the configuration class which is mentioned above.
4. Inject all the dependencies using the @Autowired annotation which we have seen in the earlier posts.
5. use AnnotationConfigApplicationContext class to create an IOC container having @Configuration class as the input class name

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

ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(AppConfig.class);

applicationContext.getBean(...); // to get the bean of particular class
//provide the class name as the parameter.

((AbstractApplicationContext)applicationContext).close();
}
}

Done application is ready and we can work with whatever beans we want and perform the business logic.

Advantages of Java-based configuration:
1. XML-based configuration can be avoided.
2. Improves readability.
3. Debugging becomes easy.
4. Foundation for spring boot.

Please do follow for more articles and learn with me.
Special Thanks to Nitin M who made these all concepts very easy.

--

--

hemasai jammana

Software Engineer, learning and exploring new technologies.