Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
In the Spring framework, a bean is an object that is instantiated, configured, and managed by the Spring IoC (Inversion of Control) container. Spring beans are defined in the Spring container and can be injected into other beans or classes.
Here are three primary ways to define and create a Spring bean:
This is the traditional way to define Spring beans. You'll need an applicationContext.xml
or any XML configuration file.
Step 1: Create your Java class.
public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message: " + message); } }
Step 2: Define your bean in the XML configuration file.
<bean id="helloWorld" class="com.example.HelloWorld"> <property name="message" value="Hello World!"></property> </bean>
Step 3: Get the bean from the Spring context.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage();
Instead of XML, you can configure the Spring container using Java.
Step 1: Create a configuration class with @Configuration
annotation.
@Configuration public class AppConfig { @Bean public HelloWorld helloWorld() { HelloWorld helloWorld = new HelloWorld(); helloWorld.setMessage("Hello World from Java Config!"); return helloWorld; } }
Step 2: Get the bean from the Java-based Spring context.
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); HelloWorld obj = context.getBean(HelloWorld.class); obj.getMessage();
Spring can automatically detect beans if you annotate them and configure component scanning.
Step 1: Annotate the class with @Component
(or @Service
, @Repository
, @Controller
, etc. depending on the type).
@Component public class HelloWorld { // ... same as above }
Step 2: Enable component scanning in XML or Java config.
For XML:
<context:component-scan base-package="com.example" />
For Java:
@Configuration @ComponentScan(basePackages = "com.example") public class AppConfig { }
Step 3: Get the bean from the Spring context.
Depending on whether you used XML or Java config for the component scan, get the bean accordingly, as demonstrated in the prior methods.
While all three methods are valid, the industry trend has been moving away from XML-based configuration in favor of Java-based configuration or component scanning, as they offer type safety and are often considered more intuitive and cleaner.
Creating Spring beans using XML configuration example:
<!-- beans.xml --> <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.xsd"> <bean id="myBean" class="com.example.MyBean"/> </beans>
public class MyBean { // Bean implementation }
Annotation-based approach for defining Spring beans:
@Component public class MyBean { // Bean implementation }
Using @Component annotation to define Spring beans:
@Component
to automatically register them as Spring beans.@Component public class MyBean { // Bean implementation }
Configuring Spring beans with @Bean annotation in Java:
@Bean
annotation in a configuration class to define beans.@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
Mixing XML and annotation-based bean creation in Spring:
<bean id="xmlBean" class="com.example.XmlBean"/>
@Component public class AnnotationBean { // Bean implementation }
Defining singleton and prototype beans in Spring XML:
<bean id="singletonBean" class="com.example.SingletonBean" scope="singleton"/> <bean id="prototypeBean" class="com.example.PrototypeBean" scope="prototype"/>
public class SingletonBean { // Singleton bean implementation } public class PrototypeBean { // Prototype bean implementation
Creating Spring beans with constructor injection in XML:
<bean id="dependencyBean" class="com.example.DependencyBean"/> <bean id="constructorBean" class="com.example.ConstructorBean"> <constructor-arg ref="dependencyBean"/> </bean>
public class DependencyBean { // Dependency bean implementation } public class ConstructorBean { public ConstructorBean(DependencyBean dependency) { // Constructor injection }
Managing dependencies in Spring beans using annotations:
@Autowired
and other annotations for dependency injection.@Component public class MyBean { @Autowired private DependencyBean dependency; // Bean implementation }
Configuring property values for Spring beans in XML:
<bean id="propertyBean" class="com.example.PropertyBean"> <property name="propertyName" value="propertyValue"/> </bean>
public class PropertyBean { private String propertyName; // Setter for propertyName }
Implementing custom initialization and destruction methods for Spring beans:
@Component public class MyBean { @PostConstruct public void customInit() { // Custom initialization logic } @PreDestroy public void customDestroy() { // Custom destruction logic } }
Using Java configuration to create Spring beans with dependencies:
@Configuration public class AppConfig { @Bean public DependencyBean dependencyBean() { return new DependencyBean(); } @Bean public MyBean myBean(DependencyBean dependencyBean) { return new MyBean(dependencyBean); } }
Conditional bean creation in Spring with annotations:
@Component @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") public class ConditionalBean { // Bean implementation }
Configuring lazy initialization for Spring beans in XML:
<bean id="lazyBean" class="com.example.LazyBean" lazy-init="true"/>
public class LazyBean { // Bean implementation }
Dynamic bean creation with factory methods in Java configuration:
@Configuration public class AppConfig { @Bean public MyBeanFactory myBeanFactory() { return new MyBeanFactory(); } @Bean public MyBean myBean() { return myBeanFactory().createMyBean(); } }
Defining aliases for Spring beans in XML configuration:
<bean id="originalBean" class="com.example.OriginalBean"/> <alias name="aliasBean" alias="originalBean"/>
public class OriginalBean { // Original bean implementation }
Creating inner beans in Spring XML configuration:
<bean id="outerBean" class="com.example.OuterBean"> <property name="innerBean"> <bean class="com.example.InnerBean"/> </property> </bean>
public class OuterBean { private InnerBean innerBean; // Setter for innerBean }