Spring Java-based Configuration Example

By | June 17, 2021

In this post, We will learn about Spring Java-based Configuration Example with a Demo Project

So far We have learned, How we can configure Spring beans using XML configuration files. If you are very much comfortable with XML configuration, then it is really not required to learn how to achieve the same result with Java-based configuration.

The java-based configuration enables us to write most of our Spring configuration without XML but with the help of few Java-based annotations explained in this post.

@Configuration  and @Bean Annotations

If We annotate a class with the @Configuration annotation means It indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring Container that a method annotated with @Bean will return an object that should be registered as a bean in the Spring Container. The very simplest possible @Configuration class would be as follows −

MySpringConfig.java

The above config code will be equivalent to the following XML configuration −

 

Message.java

 

ClientTest.java

If you run ClientTest.java as Java Application then it will give the below output: 

101 Hello World!!

That’s all about Spring Java-based Configuration Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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