Escolar Documentos
Profissional Documentos
Cultura Documentos
com
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.
<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">
</beans>
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();
}
Learn more
1. Directory Structure
See directory structure of this example.
https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 1/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com
2. Dependency Library
To use JavaConfig (@Configuration), you need to include CGLIB library. See dependencies :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
3. Spring Bean
A simple bean.
package com.mkyong.hello;
https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 2/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com
package com.mkyong.hello.impl;
import com.mkyong.hello.HelloWorld;
@Override
public void printHelloWorld(String 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;
}
}
Output
Reference
1. Spring 3 JavaConfig reference
javaconfig spring3
Discover Unexpected Indulgences
Book Now ››
Sponsored Rates From $149
mkyong
Founder of Mkyong.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
17 8 1
22
https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 4/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com
Abhinandan
Hi,
Guest 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?
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
Guest
information into database and fetch beans from database on the basis of user roles. In this case, I am using javaconfig.
kiran
It is nice but what about if we have multiple decencies in the class i.e initiated using @autowried and @values
Guest @JmsListener(destination = “${}”, containerFactory = “”).. how about these values… it is not loading. Because we are creating the object
using new operator.
Natasha
Hi,
Guest 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?
Asvin Kumar
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
Guest AnnotationConfigApplicationContext. Is it possible to load it on startup and then load the bean from the context on demand?
mohit bhagat
I downloaded this example code and imported into my Eclipse Juno. Since, I was getting a maven compile error, I built maven install on
Guest 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
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 :)
yashx1
If you will debug you will find that bean is created with name of class as “appConfig” , through which you can access methods.
Guest
Miho
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.
Guest Any idea on how to fix it?
Mahendra P
Active Intelligence
Nik
Rinkal
HI, can anyone help me. When i run my application i got this error.
Guest 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(ConfigurationClassPostProcess
or.java:249)
at
org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.jav
a:163)
at
org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:66
3)
at
org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:60
2)
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(ConfigurationClassPostProcess
or.java:241)
https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 6/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com
… 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
kiran
commons-logging-1.0.4.jar
Guest
cglib-2.2.2.jar
asm-3.1.jar
extra files needed as above… I was able to run
kiran digrase
commons-logging-1.0.4.jar
Guest
cglib-2.2.2.jar
asm-3.1.jar
extra files needed as above… I was able to run
Dan
thanooj k
javaeecub
Hi
Guest
go through this url : http://www.springsource.org/spring-gemfire
Sanjay
Google it
https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 7/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com
Guest
0 Reply 6 years ago
Richard
Shouldn’t use @Override in HelloWorldImpl because HelloWorldImpl is implementing that method, not overriding
Guest
Carnaxe
Not necessarily, in later versions of Java, it is possible to use @Override to tell the compiler that the method is written exactly
Guest
according to the header in the interface.
Abhijit Das
Hi,
Guest 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?
Spring Tutorial
[…] 3.0)Spring 3.0 supported JavaConfig, now you can use annotation to do the configuration in Spring.Spring 3 JavaConfig example
Demonstrate the use of @Configuration and @Bean to define bean in SpringSpring 3 JavaConfig @Import […]
Android Getting Started JAX London Mkyong.com is for Java and J2EE
Google App Engine – Java Java Code Geeks developers, all examples are simple and
Spring 2.5.x Documentation easy to understand, and well tested in our
Spring 3.2.x Documentation development environment.
Spring 4.1.x Documentation
Java EE 5 Tutorial Mkyong.com is created, written by, and
Java EE 6 Tutorial maintained by Yong Mook Kim, aka
https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 8/8