Methods of Bean Configurations for Spring Container


There are three ways to provide the configuration metadata to Spring container. Below are the more details about them.

1. Xml based configuration file

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<!– A simple bean definition –>
<bean id=”…” class=”…”>
<!– collaborators and configuration for this bean go here –>
</bean>

<!– A bean definition with lazy init set on –>
<bean id=”…” class=”…” lazy-init=”true”>
<!– collaborators and configuration for this bean go here –>
</bean>

<!– A bean definition with initialization method –>
<bean id=”…” class=”…” init-method=”…”>
<!– collaborators and configuration for this bean go here –>
</bean>

<!– A bean definition with destruction method –>
<bean id=”…” class=”…” destroy-method=”…”>
<!– collaborators and configuration for this bean go here –>
</bean>

<!– more bean definitions go here –>

</beans>

2. Annotation based configuration

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd”&gt;

<context:annotation-config/>
<!– bean definitions go here –>

<bean id=”empAddress” class=”in.malliktalksjava.spring.samples.EmployeeAddress” />

</beans>

Once <context:annotation-config/> is configured, you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors. Once you add the annotation config, you can use @Autowired annotation as below on setter methods to get rid of the <property> element in XML configuration file. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.

Employee.java:

package in.malliktalksjava.spring.samples;

public class Employee{

@Autowired

public EmployeeAddress empAddress;

public void test(){

System.out.println(empAddress.getStreet());

System.out.println(empAddress.getPostalCode());

}

}

EmployeeAddress.java:

package in.malliktalksjava.spring.samples;

public class EmployeeAddress{

private String street;

private String postalCode;

public void setStreet(String street){

this.street = street;

}

public String getStreet(){

return street;

}

public void setPostalCode(String postalCode)

this.postalCode = postalCode;

}

public String getPostalCode(){

return postalCode;

}

}

 

3. Java based configuration

Java based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations. Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

HelloWorldConfig.java

package in.malliktalksjava.spring.samples;
import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig {

@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}

HelloWorld.java

package in.malliktalksjava.spring.samples;

public class HelloWorld {
private String message;

public void setMessage(String message){
this.message = message;
}

public void getMessage(){
System.out.println(” Message is : ” + message);
}
}

MainApp.java:

package in.malliktalksjava.spring.samples;

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

public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);

HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

helloWorld.setMessage(“Hello World!”);
helloWorld.getMessage();
}
}

Complete application has been created without writing any configuration file.