In our previous article Spring hello world example we have used the classic XML to define our configurations. JavaConfig has been added since Spring 3.o, which lets the developer to add all the configuration into Java class rather than into XML file. Still, you have the option to add configuration in the XML format but JavaConfig is just an alternate way of adding bean definition.
Now let’s see the difference between defining bean in classic XML format and using JavaConfig.
JavaConfig way
@Configuration public class HelloWorldConfig { @Bean(name="helloWorld") public HelloWorld getHelloWorld() { return new HelloWorld(); } }
Above JavaConfig is equivalent to the below XML Configuration
Classic XML
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="helloWorld" class="com.javainterviewpoint.HelloWorld"></bean> </beans>
Lets Now see the full JavaConfig example
Folder Structure:
- Create a new Java Project “SpringCoreTutorial” and create a package for our src files “com.javainterviewpoint“
- Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.
commons-logging-1.1.1.jar
spring-beans-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar - Create the Java classes HelloWorld.java, HelloWorldConfig.java and Logic.java under com.javainterviewpoint folder.
HelloWorldConfig.java
- The @Configuration annotation indicates that this class defines one or more @Bean method which will be processed by the Spring Container to generate the Bean definitions and service at runtime.
- The @Bean annotation indicates a particular beans definition which will be processed at runtime.
package com.javainterviewpoint; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HelloWorldConfig { @Bean(name="helloWorld") public HelloWorld getHelloWorld() { return new HelloWorld(); } }
HelloWorld.java
Our HelloWorld class simply has a method display() which will be called from our Logic class
package com.javainterviewpoint; public class HelloWorld { public void display(String message) { System.out.println(message); } }
Logic.java
- Through AnnotationConfigApplicationContext we will get the context by reading our HelloWorldConfig class
- HelloWorld instance is obtained by calling the getBean() method over the applicationContext.
- The parameter passed to the getBean(“helloWorld”) should be the name defined in @Bean annotation @Bean(name=”helloWorld”)
package com.javainterviewpoint; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Logic { public static void main(String args[]) { //Reading the HelloWorldConfig through ApplicationContext ApplicationContext applicationContext =new AnnotationConfigApplicationContext(HelloWorldConfig.class); //Get the HelloWorld instance HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld"); //Call the display() method of helloWorld class helloWorld.display("Hello World via JavaConfig"); } }
Output
Upon running our Logic class we will get the below output
Hello World via JavaConfig
Leave a Reply