How many ways can you create a bean in spring?

Ways to Create a Bean in Spring

  1. XML Configuration
  2. Java-Based Configuration (using @Configuration and @Bean)
  3. Component Scanning and Stereotype Annotations
  4. Using @Component with @Bean in a Configuration Class

1. XML Configuration

In traditional Spring applications, beans are defined in XML configuration files. This method involves specifying the bean class and its properties in an XML file.

Example:

<!-- applicationContext.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="myService" class="com.example.MyServiceImpl">
<property name="myRepository" ref="myRepository"/>
</bean>

<bean id="myRepository" class="com.example.MyRepositoryImpl"/>


</beans>  

2. Java-Based Configuration (using @Configuration and @Bean)

Spring allows defining beans using Java configuration classes annotated with @Configuration. Inside these classes, beans are created using methods annotated with @Bean.

Example:

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

public class AppConfig {

@Bean
public MyService myService() {
return new MyServiceImpl(myRepository());
}

@Bean
public MyRepository myRepository() {
return new MyRepositoryImpl();
}


}  

3. Component Scanning and Stereotype Annotations

Spring supports creating beans using annotations directly on the classes, such as @Component, @Service, @Repository, and @Controller. These annotations, combined with component scanning, allow Spring to automatically detect and register beans.

Example:

import org.springframework.stereotype.Component;
@Component  

public class MyServiceImpl implements MyService {

// Service methods

}

@Component

public class MyRepositoryImpl implements MyRepository {

// Repository methods

}

Component Scanning Configuration: We need to enable component scanning with the help of @ComponentScan annotation, so that it can read the @Component annotation and register beans.

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration  

@ComponentScan(basePackages = "com.example")

public class AppConfig {

}

4. Using @Component with @Bean in a Configuration Class

You can also combine @Component for general-purpose beans and @Bean for more specific or complex bean definitions in a configuration class.

Example:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration  

@ComponentScan(basePackages = "com.example")

public class AppConfig {

@Bean
public MyService myService() {
return new MyServiceImpl();
}

@Bean
public MyRepository myRepository() {
return new MyRepositoryImpl();
}


}

@Component

public class MyOtherComponent {

// Other component methods

}