HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring 5 / Spring Core / Spring beans using annotation configuration

Spring beans using annotation configuration

Learn to create spring beans using java configuration using annotations for any standalone application. We will learn to create it with and without scanning of component annotations and using @Bean annotations.

Table of contents

1. Annotation configuration with component scanning
2. Using @Bean and @Configuration annotations

1. Annotation configuration with component scanning

Creating beans using component scanning can be done in two steps.

1.1. Annotate beans with respective component annotations

We shall use use one of following four annotations as appropriate.

  • @Component
  • @Repository
  • @Service
  • @Controller

Read More: Spring Component Annotations

package com.howtodoinjava.spring.service.impl;

import org.springframework.stereotype.Service;
import com.howtodoinjava.spring.model.Employee;
import com.howtodoinjava.spring.service.EmployeeManager;

@Service
public class EmployeeManagerImpl implements EmployeeManager {

	@Override
	public Employee create() {
		Employee emp =  new Employee();
		emp.setId(1);
		emp.setName("Lokesh");
		return emp;
	}
}

1.2. Include bean packages in @ComponentScan annotation

@Configuration
@ComponentScan(basePackages = "com.howtodoinjava.spring.service")
public class AppConfig {
	
}

1.3. Demo

package com.howtodoinjava.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.howtodoinjava.spring.model.Employee;
import com.howtodoinjava.spring.service.EmployeeManager;

public class Main 
{
    public static void main( String[] args )
    {
    	//Method 1
    	//ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
    	
    	//Method 2
    	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh();
        
    	EmployeeManager empManager = ctx.getBean(EmployeeManager.class);
    	Employee emp = empManager.create();
    	
    	System.out.println(emp);
    }
}

Program Output:

Employee [id=1, name=Lokesh]

2. Using @Bean and @Configuration annotations

To create spring application content using @Bean annotations, use these steps –

2.1. Create Java bean classes (no annotation or anything needed)

public class EmployeeManagerImpl implements EmployeeManager {

	@Override
	public Employee create() {
		Employee emp =  new Employee();
		emp.setId(1);
		emp.setName("Lokesh");
		return emp;
	}
}

2.2. Create @Bean annotation methods in configuration class

package com.howtodoinjava.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.howtodoinjava.spring.service.EmployeeManager;
import com.howtodoinjava.spring.service.impl.EmployeeManagerImpl;

@Configuration
public class AppConfig {
	
    @Bean
    public EmployeeManager employeeManager() {
        return new EmployeeManagerImpl();
    }
	
}

2.3. Demo

package com.howtodoinjava.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.howtodoinjava.spring.model.Employee;
import com.howtodoinjava.spring.service.EmployeeManager;

public class Main 
{
    public static void main( String[] args )
    {
    	//Method 1
    	//ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
    	
    	//Method 2
    	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh();
        
    	EmployeeManager empManager = ctx.getBean(EmployeeManager.class);
    	Employee emp = empManager.create();
    	
    	System.out.println(emp);
    }
}

Program Output:

Employee [id=1, name=Lokesh]

That’s pretty much two simple and easy ways to create spring beans using pure java code and annotations.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Elton Li

    September 29, 2019

    Well, it is very clear to explain the steps:
    1. Annotation based configuration with component scanning
    2. Using @Bean and @Configuration annotations

    I believe most people will use the 1st one.

Comments are closed on this article!

Search Tutorials

Spring 5 Tutorial

  • Spring 5 – Introduction
  • Spring 5 – New Features
  • Spring 5 – Bean Java Config
  • Spring 5 – Bean XML Config
  • Spring 5 – Eager vs Lazy Init
  • Spring 5 – DispatcherServlet
  • Spring 5 – MVC Annotations
  • Spring 5 – MVC + Hibernate
  • Spring 5 – CORS
  • Spring 5 – Security Java Config
  • Spring 5 – Custom Login Form

Spring Tutorial

  • Spring – Introduction
  • Spring – IoC Containers
  • Spring – IoC vs. DI
  • Spring – Bean Scopes
  • Spring – Bean Life Cycle
  • Spring – Bean Postprocessors
  • Spring – Autowiring
  • Spring – Annotations
  • Spring – Stereotype Annotations
  • Spring – Task Scheduling
  • Spring – Timer Task
  • Spring – Events
  • Spring – Message Source
  • Spring – ResourceLoader
  • Spring – Property Editor
  • Spring – Send Email
  • Spring – Version-less Schema
  • Spring – Interview Questions
  • Spring – Best Practices

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces