Spring 3 JavaConfig example

Since Spring 3, JavaConfig features are included in core Spring module, it allow developer to move bean definition and Spring configuration out of XML file into Java class.

But, you are still allow to use the classic XML way to define beans and configuration, the JavaConfig is just another alternative solution.

See the different between classic XML definition and JavaConfig to define a bean in Spring container.

Spring XML file :

<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="helloBean" class="com.mkyong.hello.impl.HelloWorldImpl">
 
</beans>

Equivalent configuration in JavaConfig :

package com.mkyong.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mkyong.hello.HelloWorld;
import com.mkyong.hello.impl.HelloWorldImpl;
 
@Configuration
public class AppConfig {
 
    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }
 
}

Spring JavaConfig Hello World

Now, see a full Spring JavaConfig example.

1. Directory Structure

See directory structure of this example.

directory structure of this example

2. Dependency Library

To use JavaConfig (@Configuration), you need to include CGLIB library. See dependencies :

	<!-- Spring 3 dependencies -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${spring.version}</version>
	</dependency>
 
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>
 
	<!-- JavaConfig need this library -->
	<dependency>
		<groupId>cglib</groupId>
		<artifactId>cglib</artifactId>
		<version>2.2.2</version>
	</dependency>

3. Spring Bean

A simple bean.

package com.mkyong.hello;
 
public interface HelloWorld {
 
	void printHelloWorld(String msg);
 
}
package com.mkyong.hello.impl;
 
import com.mkyong.hello.HelloWorld;
 
public class HelloWorldImpl implements HelloWorld {
 
	@Override
	public void printHelloWorld(String msg) {
 
		System.out.println("Hello : " + msg);
	}
 
}

4. JavaConfig Annotation

Annotate with @Configuration to tell Spring that this is the core Spring configuration file, and define bean via @Bean.

package com.mkyong.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mkyong.hello.HelloWorld;
import com.mkyong.hello.impl.HelloWorldImpl;
 
@Configuration
public class AppConfig {
 
    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }
 
}

5. Run it

Load your JavaConfig class with AnnotationConfigApplicationContext.

package com.mkyong.core;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.mkyong.config.AppConfig;
import com.mkyong.hello.HelloWorld;
 
public class App {
	public static void main(String[] args) {
 
            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
	    HelloWorld obj = (HelloWorld) context.getBean("helloBean");
 
	    obj.printHelloWorld("Spring3 Java Config");
 
	}
}

Output

Hello : Spring3 Java Config

Download Source Code

Download It – Spring3-JavaConfig-Example.zip (6 KB)

Reference

  1. Spring 3 JavaConfig reference
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: weblink()

  • Pingback: I was reading this()

  • Pingback: alkaline water()

  • Pingback: pay plan()

  • Pingback: water ionizer()

  • Pingback: alkaline water brands()

  • Pingback: water ionizer()

  • Pingback: water ionizer()

  • Pingback: electrical tools list()

  • Pingback: electricians journey mods()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: double d locksmith sandy oregon()

  • Pingback: locksmithing picks scoresandodds()

  • Pingback: our site()

  • Pingback: visite site()

  • Pingback: pay day loans()

  • Pingback: stop parking()

  • Pingback: paypal loans()

  • Pingback: parking()

  • Pingback: Blue Coaster33()

  • Pingback: Active Intelligence()

  • Nik

    I have a question about closing the context.
    For web application should we create new AnnotationConfigApplicationContext(classname.class) for each request? If so is it required to close the context?

  • Rinkal

    HI, can anyone help me. When i run my application i got this error.
    org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@126f827: startup date [Sat Sep 01 22:34:26 IST 2012]; root of context hierarchy
    Exception in thread “main” java.lang.IllegalStateException: Cannot load configuration class: com.configration.AppConfig
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:249)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:163)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:602)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:65)
    at com.run.RunApplication.main(RunApplication.java:20)
    Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/Type
    at net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:180)
    at net.sf.cglib.core.KeyFactory.(KeyFactory.java:66)
    at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69)
    at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer(ConfigurationClassEnhancer.java:101)
    at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance(ConfigurationClassEnhancer.java:89)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:241)
    … 6 more
    Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    … 12 more
    Java Result: 1

    • Rajesh Kumar

      Download com.springsource.org.objectweb.asm-3.1.0.jar from http://www.docjar.com/jar_detail/com.springsource.org.objectweb.asm-3.1.0.jar.html and include in classpath. It worked for me.

    • http://www.kjnext.com kiran

      commons-logging-1.0.4.jar

      cglib-2.2.2.jar

      asm-3.1.jar
      extra files needed as above… I was able to run

    • http://www.kjnext.com kiran digrase

      commons-logging-1.0.4.jar
      cglib-2.2.2.jar
      asm-3.1.jar
      extra files needed as above… I was able to run

  • Dan

    Love your tutorials, simple and to the point. Thanks.

  • thanooj k

    could you please update me on..
    Spring javaConfig using gemfire cache abstraction.
    actually, i am done with XML based gemfire caching, but i need to work with gemfire using JavaConfig.

    here is my XML code:

    now i want the XML equivalent JavaConfig

    Please do the needful.
    thanks in advance.

  • Richard

    Shouldn’t use @Override in HelloWorldImpl because HelloWorldImpl is implementing that method, not overriding

    • Carnaxe

      Not necessarily, in later versions of Java, it is possible to use @Override to tell the compiler that the method is written exactly according to the header in the interface.

  • Abhijit Das

    Hi,
    It’s awesome tutorial I ever had and having good explation for all the critical topics.
    Could you please add tutorial on Spring Batch jobs?

  • Abhinandan

    Hi,

    My question is, the whole idea of configuration files was to reduce code. With JavaConfig we are increasing the coding overhead. We again have to deal with compile-build issues. What is the real take away of using JavaConfig?

    • x

      some people prefer java classes over xml config files :)

    • Amreesh Tyagi

      If you want to create dynamic beans than you have to go for java classes. I am developing an application where I store beans information into database and fetch beans from database on the basis of user roles. In this case, I am using javaconfig.

  • Pingback: Spring Tutorial()