Você está na página 1de 8

3/8/2019 Spring 3 JavaConfig example – Mkyong.

com

Spring 3 JavaConfig example


By mkyong | June 17, 2011 | Viewed : 317,574 | +267 pv/w

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.

Demo Aspen Mesh™ - Deploy to K8s in


Minutes
AdSee How Simple it is to Deploy the Aspen Mesh™
Agent into Your Kubernetes Cluster
aspenmesh.io

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 :

<!-- 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);

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;

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)


https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 3/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com

Reference
1. Spring 3 JavaConfig reference

javaconfig spring3

HOW MUCH DO YOU KNOW ABOUT ROBOTS? 

How was the rst working robot


employed in 1961? 15

A Searching for gold

B Spying on Russian-American families

C Watering at the Olive Garden


D Making cars for Ford Your Score 0 Question 1/10


Discover Unexpected Indulgences
Book Now ››
Sponsored Rates From $149

About the Author

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

Join the discussion...

17  8 1    

22

 newest  oldest  most voted

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?

 4    Reply  7 years ago 

some people prefer java classes over xml config files :)


Guest

 0   Reply  6 years ago

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.

 4   Reply  6 years ago

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.

 0    Reply  11 months ago

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?

 0    Reply  11 months ago

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?

 0    Reply  3 years ago

mohit bhagat

thank you for the best tutorial


Guest

 0    Reply  3 years ago

Ajay Chowdary Kandula

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

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…


https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 5/8
3/8/2019 Spring 3 JavaConfig example – Mkyong.com

 0    Reply  3 years ago

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

 0    Reply  4 years ago

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?

 0    Reply  4 years ago

Mahendra P

good examples for the beginners explanation is good


Guest

 0    Reply  5 years ago

Active Intelligence

[…] Spring 3 JavaConfig example – Mkyong.com – mkyong.com – Viewed 8 times […]

 0    Reply  5 years ago

Nik

I have a question about closing the context.


Guest For web application should we create new AnnotationConfigApplicationContext(classname.class) for each request? If so is it required to
close the context?

 0    Reply  5 years ago

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

 0    Reply  6 years ago 

Rajesh Kumar

Download com.springsource.org.objectweb.asm-3.1.0.jar from


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

 1   Reply  6 years ago

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

 0   Reply  5 years ago

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

 0   Reply  5 years ago

Dan

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


Guest

 0    Reply  6 years ago

thanooj k

could you please update me on..


Guest 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.

 0    Reply  6 years ago 

javaeecub

Hi
Guest
go through this url : http://www.springsource.org/spring-gemfire

 0   Reply  6 years ago 

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

 0    Reply  7 years ago 

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.

 0   Reply  7 years ago

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?

 0    Reply  7 years ago

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 […]

 0    Reply  7 years ago

Favorites Links Partners & Bookmarks About Mkyong.com

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

Java EE 7 Tutorial Mkyong.


Java 6 API
Java 7 API
Java 8 API
Oracle J2SE Tutorials
JSF Home Page
JSP Home Page
Maven Central Repository
Gradle User Guide
Hibernate ORM
JAX-WS Home Page
JAX-RS Home Page (Jersey)
Tomcat 8 Documentation

Copyright © 2008-2019 Mkyong.com, all rights reserved. Privacy Policy

https://www.mkyong.com/spring3/spring-3-javaconfig-example/ 8/8

Você também pode gostar