0% found this document useful (0 votes)
20 views11 pages

Spring @configuration Annotation Example

Uploaded by

Akanksha Ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Spring @configuration Annotation Example

Uploaded by

Akanksha Ojha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

14/04/2024, 19:35 Spring @Configuration annotation example

Spring @Configuration
annotation example
Lokesh Gupta
November 14, 2022

Spring Core

Spring Configuration

Spring @Configuration annotation helps in Spring annotation based configuration.


@Configuration annotation indicates that a class declares one or more @Bean
methods and may be processed by the Spring container to generate bean definitions
and service requests for those beans at runtime.

Since spring 2, we were writing our bean configurations to xml files. But Spring 3 gave
the freedom to move bean definitions out of xml files. we can give bean definitions in
Java files itself. This is called Spring Java Config feature (using @Configuration
annotation).

1. Spring @Configuration annotation usage

Use @Configuration annotation on top of any class to declare that this class provides
one or more @Bean methods and may be processed by the Spring container to
generate bean definitions and service requests for those beans at runtime.

@Configuration
public class AppConfig {

@Bean(name="demoService")
public DemoClass service()
{

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 1/11
14/04/2024, 19:35 Spring @Configuration annotation example

}
}

2. Spring @Configuration annotation example

To understand @Configuration annotation usage, let’s see it in action.

2.1. Create maven project

mvn archetype:generate
-DgroupId=com.howtodoinjava.core
-DartifactId=springCoreTest
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

mvn eclipse:eclipse

2.2. Update Spring dependencies

Update maven dependencies. I have added Spring 5 deendencies.

<properties>
<spring .version>5.0.6.RELEASE</spring>
https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 2/11
14/04/2024, 19:35 Spring @Configuration annotation example

</properties>

<!-- Spring 5 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>

<!-- For JavaConfig -->


<dependency>
<groupid>cglib</groupid>
<artifactid>cglib</artifactid>
<version>2.2.2</version>
</dependency>

2.3. Create spring beans

public interface DemoManager {


public String getServiceName();
}

public class DemoManagerImpl implements DemoManager


{
@Override
public String getServiceName()
{
return "My first service with Spring 3";
}
}

2.4. Spring configuration class with @Configuration annotation

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.howtodoinjava.core.beans.DemoManager;
import com.howtodoinjava.core.beans.DemoManagerImpl;
https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 3/11
14/04/2024, 19:35 Spring @Configuration annotation example

@Configuration
public class ApplicationConfiguration {

@Bean(name="demoService")
public DemoManager helloWorld()
{
return new DemoManagerImpl();
}
}

3. Demo

Lets write the test code and run. This should be able to configure bean and we should
be able to use it.

package com.howtodoinjava.core.verify;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContex

import com.howtodoinjava.core.beans.DemoManager;
import com.howtodoinjava.core.config.ApplicationConfiguration;

public class VerifySpringCoreFeature


{
public static void main(String[] args)
{
ApplicationContext context = new AnnotationConfigApplicationContext(Applicat

DemoManager obj = (DemoManager) context.getBean("demoService");

System.out.println( obj.getServiceName() );
}
}

Download Sourcecode

Happy Leaning !!

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 4/11
14/04/2024, 19:35 Spring @Configuration annotation example

Comments

 Subscribe 

Join the discussion

{} [+] 

3 COMMENTS   Most Voted 

View Comments

Search …

Weekly Newsletter

Stay Up-to-Date with Our


Weekly Updates. Right into
Your Inbox.

Email Address

Subscribe

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 5/11
14/04/2024, 19:35 Spring @Configuration annotation example

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 6/11
14/04/2024, 19:35 Spring @Configuration annotation example

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 7/11
14/04/2024, 19:35 Spring @Configuration annotation example

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 8/11
14/04/2024, 19:35 Spring @Configuration annotation example

About Us

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 9/11
14/04/2024, 19:35 Spring @Configuration annotation example

HowToDoInJava provides tutorials and how-to guides on Java and related


technologies.

It also shares the best practices, algorithms & solutions and frequently
asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Dark Mode

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 10/11
14/04/2024, 19:35 Spring @Configuration annotation example

Copyright © 2024 · Hosted on Cloudways · Sitemap

https://howtodoinjava.com/spring-core/spring-configuration-annotation/ 11/11

You might also like