Pages Navigation Menu

Coding is much easier than you think

Spring – Java config file providing the configuration metadata

 
spring logo
 
download
 
In the previous example, we observed the working of spring at its simplest . The main part of injecting the dependency value here, was taken up by the Configuration metadata xml file (Beans.xml). Another option supported by spring is to set this information through a java configuration file.
 
** UPDATE: Spring Complete tutorial now available here.
 

1 Create the POJO €“ HelloWorld.java

 

File : HelloWorld.java
 

package com.simpleCodeStuffs;

public class HelloWorld {

public HelloWorld(){

System.out.println("In constructor of HelloWorld");

}

private String message;

public void setMessage(String message){

this.message  = message;

}

public void getMessage(){

System.out.println("Your Message : " + message);

}

}

 

Step 2 :

 
Previously, the metadata was provided through xml (Beans.xml)using various xml tags. Now let us define the java configuration equivalent of the same which makes use of annotations to provide the configuration data. Create a new class JavaConfig under the same package.

The configuration metadata is provided through a java configuration class, using annotations

File :JavaConfig.java

 

package com.simpleCodeStuffs;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class JavaConfig {

@Bean(name="helloWorld")

public HelloWorld helloWorld(){

HelloWorld bean = new HelloWorld();

System.out.println("helloWorld bean through Java Config");

bean.setMessage("JavaConfig msg- SimpleCodeStuffs");

return bean;

}

}

 

AnnotationDescription
@ConfigurationStates that this class is used by Spring to provide bean definition. An application may have one or more classes with @Configuration
@Beanattributes :- 

a) Name

b) initMethodName

c) destroyMethodName

d) Scope

It is a method level annotation. The return value of the methods annotated with @Bean is registered as a bean by BeanFactory.a) This is used to explicitly mention the name of the bean. By default the bean name is same as the method name.b) The method to be called for the bean initialization

c) The method to be called at the end of the bean lifecycle.

d) Defines the scope of bean. By default it is taken as ‘singleton’.

 

@importThe functionality is similar to <import resource=”..”/> in xml file.

 

In case of dependencies between beans, then in place of using ref=€<beanID>€ in the xml config, here a method call is placed between the beans.

 
This can be done as follows
 
File : JavaConfig.java
 
@Configuration

public class JavaConfig {

@Bean

public Candidate candidate() {

return new Candidate(address());

}

@Bean

public Address address() {

return new Address ();

}

}

 
In this case, Address is an attribute inside Candidate. Hence the value for the Address bean is injected by passing a call to address() bean method from Candidate’€™s bean method.
 

3. Create a main class for running the above example

 

File : MainJavaConfig.java

package com.simpleCodeStuffs;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainJavaConfig {

public static void main(String args[]){

ApplicationContext context =
           new AnnotationConfigApplicationContext(JavaConfig.class);

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

obj.getMessage();

}

}

 

4 : Demo

Run the Main class. The output is as follows

 

JavaConfigOutput

 
download