Annotation based Configuration in Spring Framework Example

By | June 17, 2021

In this post, We will learn about Annotation-based Configuration in Spring Framework with an Example

In Spring Framework,  We can use an annotation-based configuration instead of using XML config for defining the beans wiring, We have an option to move the beans configuration into component class. This can be done by using annotations on the component class, method, or field declaration. One thing you should keep in mind that before performing XML injection annotation injection is performed so the latter one(annotation based) will override the former configuration(XML configuration).

In Spring Framework annotation wiring is not enabled by default so We need to enable it before we can use the Spring annotation-based wiring in the Spring Configuration file.

pom.xml

Message.java

ApplicationContext.xml

Consider the following configuration file in case we want to use any annotation in our Spring application then we need to enable it using the following XML tags

Once We add

<context:annotation-config/>

or 

<context:component-scan base-package=”com.kkhindigyan.model”></context:component-scan>

in our XML config afterward, We can start annotating our code to indicate that Spring should automatically wire values into properties, methods, and constructors.

ApplicationContext.xml

ClientTest.java

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

101 Welcome to Spring Framework!!

That’s all about Annotation based Configuration in Spring Framework Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Spring Java-based Configuration 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.