• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Spring configuration using java

June 5, 2017 By j2eereference 1 Comment

Spring configuration using java:

Here we are going to see how to  do spring configuration using java , without spring configuration xml . There are some annotations used for configuring the Beans .  You can have one or many configuration classes . The configuration classes should be annotated with @Configuration and all the beans should be annotated with @Bean .

Lets understand this by a sample configuration .

Java
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class mySpringConf {
 
    @Bean
    public HelloWorld helloWorld() {
        return new HelloWorld();  // instantiate helloWorld object 
    }
}

Java
1
2
3
4
5
6
7
8
9
10
11
public class HelloWorld {
String message;
 
public String getMessage() {
return message;
}
 
public void setMessage(String message) {
this.message = message;
}
}

Java
1
2
3
4
5
6
7
8
9
10
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class SpringConfDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(mySpringConf.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Welcome to j2eereference.com");
System.out.println(helloWorld.getMessage());
}
}

Output

May 30, 2017 5:17:28 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue May 30 17:17:28 IST 2017]; root of context hierarchy

Welcome to j2eereference.com

Here you can notice the following:

  • Created mySpringConf class for configuring the beans ,Each beans should be annotated as @Bean.
  • Created the AnnotationConfigApplicationContext object by providing the configuration class name ( mySpringConf .class) in the SpringConfDemo class
  • Got the Helloworld object using the getBean method of AnnotationConfigApplicationContext

Bean aliasing 

We can have multiple names for a bean and which can be configured as below.

@Bean(name = { “name-1”, “name-2”, “name-3” })

or

@Bean( { “name-1”, “name-2”, “name-3” })

Java
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class mySpringConf {
 
@Bean({"name-1", "name-2", "name-3"})
public HelloWorld helloWorld() {
return new HelloWorld();
}
}

and the bean can be accessed by calling getBean() method with bean name as parameter . It could be name-1 ,name-2 or name-3 as below

Java
1
2
3
4
5
6
7
8
9
10
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class SpringConfDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(mySpringConf.class);
HelloWorld helloWorld = (HelloWorld) ctx.getBean("name-1");
helloWorld.setMessage("Welcome to j2eereference.com");
System.out.println(helloWorld.getMessage());
}
}

Bean description

We can configure the bean description with annotation. This is particularly useful when beans are exposed (Example: via JMX) for monitoring purposes.

@Bean
@Description(“Description about the bean”)

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring
  • Spring JSR-250 Annotations

Filed Under: Spring

Reader Interactions

Trackbacks

  1. Configuring Spring Bean Scope with java - J2EE Reference says:
    June 7, 2017 at 12:12 am

    […] the previous post we have explained how to configure spring with java . Here we are going to see how to configure the bean scope using Java […]

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.