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.
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
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?
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.
some people prefer java classes over xml config files 🙂
It is nice but what about if we have multiple decencies in the class i.e initiated using @autowried and @values
@JmsListener(destination = “${}”, containerFactory = “”).. how about these values… it is not loading. Because we are creating the object using new operator.
Hi,
I got that, to create only one bean, we can use @Bean(name=”helloBean”) and return a single instance of HelloWorldImpl like in “return new HelloWorldImpl()”
If we need to create multiple beans we need to use @Bean({“b1”, “b2”}). How can we create b1 and b2 and return their instances?
One question, I want to develop an application where I can’t have main, I’m developing in Spring MVC and I just want to use the AnnotationConfigApplicationContext. Is it possible to load it on startup and then load the bean from the context on demand?
thank you for the best tutorial
I downloaded this example code and imported into my Eclipse Juno. Since, I was getting a maven compile error, I built maven install on command prompt, on refreshing the pronect in Eclipse Juno, saw errors
Syntax error, annotations are only available if source level is 1.5 or greater
Deleted the project from Juno
so added, the below property in bold and ran mvn install again…
3.0.5.RELEASE
1.6
Now, imported into Eclipse Juno, which resolved all the 4 errors shown before 🙂
Thanks Mykyong and Team.
Posting for others, just FYI…
If you will debug you will find that bean is created with name of class as “appConfig” , through which you can access methods.
When ever I try to run it I get this “Error: Could not find or load main class com.mkyong.core.App”. I have no idea what is causing this. Any idea on how to fix it?
good examples for the beginners explanation is good
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?
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
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
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
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.
Love your tutorials, simple and to the point. Thanks.
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.
Hi
go through this url : http://www.springsource.org/spring-gemfire
Google it
Shouldn’t use @Override in HelloWorldImpl because HelloWorldImpl is implementing that method, not overriding
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.
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?