Understanding Spring Beans

        Spring Beans refers to the objects managed and maintained inSpring IoC container, and its function is similar toObject instance in Java. In the Spring framework, all application objects can be called beans and are created, configured and managed by the Spring IoC container.

        Usually, when using Spring IoC container objects, you need to define one or more Bean objects first, and then the container generates corresponding Bean instances based on these definition information. Each bean has its own name and type, which can be injected into other beans within the container, and the bean instance can also be obtained through the name.

        In Spring, the creation and management of Beans and the management of dependencies between Beans are implemented using Inversion of Control (IoC) and Dependency Injection (DI) technologies. Developers only need to declare component dependencies in configuration files or Java code, and the container will be automatically created, initialized, and injected at runtime.

        For example, we have a business logic component named UserService, and the UserDao data access component it depends on is incorporated into the Spring IoC container for management:

@Component
public class UserService {
    @Autowired
    private UserDao userDao;
    //...
}

@Component
public class UserDao{
  //...
}

        In the above code, both `UserDao` and `UserService` use the `@Component` annotation to identify them as Spring Beans, and the `@Autowired` annotation is used in `UserService` to implement dependency injection of `UserDao`.

        In general, Spring Beans can be defined and managed through XML configuration, Java Config or annotation-based methods, and has a wide range of functions and can be used in business logic, data access, AOP aspects, MVC controllers and other aspects. At the same time, compared with conventional object instances, Spring Beans has a more flexible life cycle and dependency injection mechanism, which can better support the rapid and stable development of applications.

Bean configuration annotations in spring boot

        In Spring Boot, it is still very common to use `@Component` annotations because they are still a flag used inside the Spring IoC container. However, in some cases we may choose to omit this annotation.

        ​​​​​Through Spring Boot’s autowiring feature, we can easily complete the automatic registration and dependency injection of beans. For example, if we need to include a service component into a container to ensure that it can be dependency injected by other components, we can use `@Service`, `@Repository`, `@Controller` and other annotations to achieve this. The sample code is as follows:

@Service
public class UserService {
    // ...
}

        In the above code, the `@Service` annotation is used to identify the bean as a service component, which is managed and injected by the Spring container.

        In addition, we can also use the `@Configuration` and `@Bean` annotations to define the Java Config type configuration class and the beans in it. These beans will also be automatically managed by the Spring container and can be used for dependency injection by other components. For example:

@Configuration
public class AppConfig {

    @Bean
    public UserService userService() {
        return new UserService();
    }

}

 

        In the above code, `UserRepository` is registered as a Spring Bean and placed in the application context for dependency injection by other components.

        Therefore, in general, using `@Component` in Spring Boot has the same effect as using other exclusive annotations, which mainly depends on personal programming habits and habits. Using `@Component` will make the code more versatile and reusable, while other annotations can better semantically reflect the roles and responsibilities of each bean. Moreover, the Spring Boot framework also provides automatic assembly functions, which can avoid manual configuration and cumbersome bean dependency injection operations.

Guess you like

Origin blog.csdn.net/weixin_52060913/article/details/130531563