JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

@Configuration Annotation in Spring 3.0

October 27, 2013 by Krishna Srinivasan Leave a Comment

Spring 3.0 has introduced new way for configuring the spring beans. In fact this option was maintained by spring framework separately as JavaConfig project. Later this project is merged to the spring core framework and all the features are directly accessible from the spring core. If you are beginner in spring framework, we have huge collection of spring tutorials and spring 4 tutorials for your reference.

Instead of using the XML files, we can use plain Java classes to annotate the configurations by using the @Configuration annotation. If you annotate a class with @Configuration annotation, it indicates that the class is used for defining the beans using the @Bean annotation. This is very much similar to the <bean/> element in the spring XML configurations.

Additional Reading:

  • Spring Boot Tutorials
  • Spring Data JPA Tutorial

This approach is not 100% replacement for the XML configurations. It is upto the developer’s choice to choose between JavaConfig and XML configurations. In most cases, the bootstrap configuration will be done using the XML file and then JavaConfig will be used for adhoc requirements.

The following are the list of annotations introduced as part of the JavaConfig module.

  1. @Configuration
  2. @Bean
  3. @DependsOn
  4. @Primary
  5. @Lazy
  6. @Import
  7. @ImportResource
  8. @Value

In this example I have used only the first two annotations to demonstrate the basic use of JavaConfig features.

AnnotationConfigApplicationContext is the class used for loading the configuration from Java class file. Look at the below example. If you have any questions, please write it in the comments section.

1. Create JavaConfig

The following are the twp steps required for creating the Java configuration classes in the spring framework.

  • Annotate the class with @Configuration annotation
  • Annotate the method with @Bean to indicating that it is bean definition

[code lang=”java”]
package javabeat.net;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JavaConfig {
@Bean(name="userDetails")
public UserDetails userDetails(){
return new UserDetails();
}
}
[/code]

2. Create Bean Class

[code lang=”java”]
package javabeat.net;

public class UserDetails {
private String name;
private String phone;
private String city;
private String country;

// Other methods

}
[/code]

3. Load Application Context and Instantiate Bean

Create AnnotationConfigApplicationContext and get the bean instance. This is very simple example to show you how simple to configure the beans using Java class.

[code lang=”java”]

package javabeat.net;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class JavaConfigDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
UserDetails userDetails = (UserDetails) context.getBean("userDetails");
}
}

[/code]

I hope this article have helped you to understand the use of @Configuration annotation and how to use them. This has been taken as the recommended way for writing the configurations for spring applications.

Filed Under: Spring Framework Tagged With: Spring Annotations

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved