Spring Bean
A Spring Bean is an object managed by the Spring framework. It is created by the Spring IoC container and is managed throughout the lifecycle of an application.
How to Create a Spring Bean?
A Spring Bean can be defined using XML configuration and annotations. In the examples below, we will use annotations since these have become a very common and modern way of defining a bean in Spring framework. Annotation-based configuration provides a more concise and easy-to-read way of defining beans, compared to traditional XML-based configuration.
The most common annotations used for defining a bean are @Component, @Service, @Repository, @Controller and @Bean.
- @Component is a general-purpose, class-level stereotype annotation that indicates that a class is a component in the Spring application. The @Component annotation can be used to mark any class as a candidate for auto-detection as a Spring Bean.
@Component
public class Employee {
// ...
}
- @Controller is a specialization of @Component that is used to indicate that a class is a controller component. The @Controller annotation is used to mark a class that provides HTTP request handling services.
@Controller
public class EmployeeController {
// ...
}
- @Service is a specialization of @Component that is used to indicate that a class is a service component. The @Service annotation is used to mark a class that provides business logic services.
@Service
public class EmployeeService {
// ...
}
- @Repository is a specialization of @Component that is used to indicate that a class is a repository component. The @Repository annotation is used to mark a class that provides data access services.
@Repository
public interface EmployeeRepository extends CrudRepository<T, ID>{
// ...
}
- @Bean annotation is used to define a bean in Spring framework, specifically in a Java configuration class. This a method-level annotation that is used in conjunction with the @Configuration annotation to define beans and manage their lifecycle within the Spring IoC container. Notice that the body of the method contains the logic responsible for creating the instance.
@Configuration
public class AppConfig {
@Bean
public EmployeeService employeeService() {
return new EmployeeService();
}
}
Spring Bean Scope
Spring Beans have several different scopes that define the lifecycle and scope of the bean within the Spring IoC container. The most common scopes are:
- Singleton: This is the default scope for a Spring Bean, and it means that there is only one instance of the bean in the IoC container. This instance is shared by all objects that request it.
- Prototype: A prototype scope means that a new instance of the bean is created each time it is requested. This is useful for beans that are stateful or have a large amount of data.
- Request: This scope is used for beans that are associated with an HTTP request. A new instance of the bean is created for each request, and the bean is only accessible within the scope of that request.
- Session: This scope is used for beans that are associated with an HTTP session. A new instance of the bean is created for each session, and the bean is only accessible within the scope of that session.
- Global Session: This scope is used for beans that are associated with a global HTTP session, typically in a Portlet context.
Spring Bean vs Java Bean
A Java Bean is a simple Java class that follows certain conventions, such as having a no-arg constructor, private fields, and public getter and setter methods for accessing those fields. Java Beans are often used to store data and transfer data between different parts of an application.
// example of a Java bean
public class Employee {
private String name;
private int age;
// No-arg constructor
public Employee() { }
// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
In this example, the Employee class is a simple Java Bean that follows the conventions of having a no-arg constructor, private fields, and public getter and setter methods.
A Spring Bean, on the other hand, is an object managed by the Spring framework, specifically by the Spring IoC (Inversion of Control) container. A Spring Bean is created by the IoC container and is managed throughout the lifecycle of an application. It can be a simple Java Bean, but it is also commonly used to represent services, repositories, controllers, and other types of components within a Spring-based application.
// example of a Spring Bean
@Service
public class EmployeeService {
private EmployeeDao employeeDao;
@Autowired
public EmployeeService(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public List<Employee> getAllEmployees() {
return employeeDao.findAll();
}
}
In this example, the EmployeeService class is a Spring Bean that is annotated with @Service. This annotation marks the class as a service component, and it will be automatically discovered and managed by the Spring IoC container. The EmployeeService class also has a constructor that is annotated with @Autowired, which will allow Spring to inject an instance of EmployeeDao into the bean when it is created.
Top comments (0)