Spring java based configuration example

Previous
Next
In this post , we will see how to configure spring with java based configuration. In previous post, we have already seen how to configure spring using xml configuration. Spring provides @configuaration and @Bean annotation for java based configuration.

Steps for spring java based configuration:

Project structure for below project:

1. Create a simple java maven project.

2. Maven dependency

put spring and cglib maven dependency in pom.xml.

So your pom.xml will look like:

3. Create Bean class

Create a bean class called country.java in package org.arpit.java2blog.model .

4. Create application configuration class

This class will have @Configuaration and @Bean annotation .

Create class called ApplicationConfiguration.java in package org.arpit.java2blog.config

Above file is equivalent to below spring configuration xml

5. Create main class to run the program

Create class called SpringJavaConfigMain.java

6. Run above program

When you run above program, you will get below output.

7. Source code

click to begin
20KB .zip

Bingo!! we are done with spring java based configuaration.

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog