6 ways to create spring beans.

Creating Spring beans in a Spring Boot application can be done using several methods, similar to those in a regular Spring application. Here’s a detailed guide on how to create Spring beans in Spring Boot

1. Using @Component and Stereotype Annotations

The simplest way to create a Spring bean in Spring Boot is to use the @Component annotation or other stereotype annotations like @Service, @Repository, and @Controller. These annotations are typically used on classes to indicate that they are Spring-managed beans.

// Marking the class as a Spring-managed component
@Component
public class MyComponent {
// Implementation code
}

// Using stereotype annotation for service layer
@Service
public class MyService {
// Implementation code
}

// Using stereotype annotation for repository layer
@Repository
public class MyRepository {
// Implementation code
}

// Using stereotype annotation for controller layer
@Controller
public class MyController {
// Implementation code
}

2. Using @Configuration and @Bean Annotations

Another way to create beans is by using a configuration class with @Configuration and defining beans using the @Bean annotation.

@Configuration
public class AppConfig {

@Bean
public MyBean myBean() {
return new MyBean();
}
}

3. Using @SpringBootApplication

When you use the @SpringBootApplication annotation, it automatically scans the package where the application class is located and its sub-packages for Spring components. This is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan together.

@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}

4. Using @Autowired for Dependency Injection

Once beans are created, you can inject them into other beans using @Autowired. Spring Boot supports constructor injection, field injection, and setter injection.

Constructor Injection (Preferred)

@Service
public class MyService {

private final MyRepository myRepository;

@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}

// Implementation code
}

Field Injection

@Service
public class MyService {

@Autowired
private MyRepository myRepository;

// Implementation code
}

Setter Injection

@Service
public class MyService {

private MyRepository myRepository;

@Autowired
public void setMyRepository(MyRepository myRepository) {
this.myRepository = myRepository;
}

// Implementation code
}

5. Using @Conditional Annotations

You can conditionally create beans using @Conditional annotations such as @ConditionalOnProperty, @ConditionalOnClass, etc.

@Configuration
public class ConditionalConfig {

@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public MyFeatureBean myFeatureBean() {
return new MyFeatureBean();
}
}

6. Using Profiles

Spring Boot allows you to create beans for specific profiles using the @Profile annotation. This is useful for defining beans that should only be loaded in certain environments (e.g., development, production).

@Configuration
@Profile("dev")
public class DevConfig {

@Bean
public MyDevBean myDevBean() {
return new MyDevBean();
}
}

@Configuration
@Profile("prod")
public class ProdConfig {

@Bean
public MyProdBean myProdBean() {
return new MyProdBean();
}
}

Conclusion

Creating Spring beans in Spring Boot is straightforward and follows the principles of the core Spring framework. By using annotations such as @Component, @Service, @Repository, @Controller, @Configuration, and @Bean, you can define and manage your beans efficiently. Additionally, leveraging dependency injection with @Autowired and conditional configurations with profiles and @Conditional annotations ensures that your application is flexible, maintainable, and scalable.


6 ways to create spring beans. was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.

This post first appeared on Read More