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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kkhindigyan.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.kkhindigyan.model.Message; @Configuration public class MySpringConfig { //Here Spring bean id would be message @Bean(name="message") public Message getMessage() { return new Message(); } } |
The above config code will be equivalent to the following XML configuration −
1 2 3 |
<beans> <bean id = "message" class = "com.kkhindigyan.model.Message" /> </beans> |
Message.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kkhindigyan.model; public class Message { private Integer messageId; private String message; public Integer getMessageId() { return messageId; } public void setMessageId(Integer messageId) { this.messageId = messageId; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import com.kkhindigyan.config.MySpringConfig; import com.kkhindigyan.model.Message; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new AnnotationConfigApplicationContext(MySpringConfig.class); //Asking Spring Container to return Spring bean with id "message" Object object = applicationContext.getBean("message"); //Covert Spring bean into your business Object Message message = (Message)object; message.setMessageId(101); message.setMessage("Hello World!!"); //Print Spring bean state System.out.println(message.getMessageId()+"\t"+message.getMessage()); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
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.