Introduction to annotations in springboot

beans in springboot

In the Java programming language, "Bean" usually refers to a Java Bean, which is an ordinary Java class that conforms to a specific specification. The Java Bean class has the following characteristics:

  1. Encapsulation: Java Beans use private fields (member variables) and public Getter and Setter methods to encapsulate data to achieve access control of properties.

  2. No-argument constructor: Java Beans usually contain a no-argument constructor, which is used to create Bean objects.

  3. Serializable: Java Beans can implement the Serializable interface to support serialization and deserialization operations of objects.

  4. Property accessor: Properties in the Java Bean class are accessed through Getter and Setter methods. The Getter method is used to obtain the property value, and the Setter method is used to set the property value.

The design purpose of Java Bean is to simplify the development and maintenance of Java programs so that programmers can access and operate the properties of objects in a unified way. Java Beans are widely used in Java EE (Enterprise Edition Java), such as in frameworks such as JavaServer Faces (JSF) and Spring Framework, as well as in Java graphical interface development (such as JavaFX).

In short, Java Bean is an ordinary Java class that conforms to specific specifications. Through features such as encapsulation, attribute accessors, and serializability, it provides a standardized programming model to facilitate the development and maintenance of Java programs.

In the Spring framework, "bean" is a very important concept. It is an object managed by the Spring container. Spring beans are created and managed through dependency injection (Dependency Injection).

In Spring, Java classes (POJOs) or other Spring framework components can be declared as beans. The Spring container marks these classes as beans through configuration files (such as XML configuration files) or using annotations, and is responsible for creating, initializing, and managing their life cycles.

Spring beans have the following characteristics:

  1. Configurability: Beans can be defined and configured in configuration files according to the needs of the application. Configuration files tell the Spring container how to create and manage these beans.

  2. Dependency injection: The Spring container decouples dependencies between beans through dependency injection. That is, when creating a bean, Spring will automatically inject other beans it depends on through the constructor, Setter method or field.

  3. Life cycle management: The Spring container is responsible for managing the life cycle of the bean, such as performing specific operations when the bean is created, initialized, and destroyed. You can add custom logic at different stages of the bean life cycle by defining initialization methods and destruction methods.

  4. AOP support: The Spring framework provides support for aspect-oriented programming (AOP), which can decouple cross-cutting concerns (such as logs, transactions, etc.) from business logic through configuration to improve the modularity and maintainability of the code.

Spring's bean management makes the collaboration between application components more flexible and loosely coupled. Through configuration files or annotations, the Spring container can dynamically create and manage beans as needed, realizing the design principles of Inversion of Control (IoC) and Dependency Injection (DI), which greatly simplifies application development and maintenance.

In the Spring Boot framework, there are many commonly used annotations used to mark and configure different components. These annotations are scanned and processed by the Spring Boot framework through the reflection mechanism. The following are some common annotations, their roles and life cycles:

@SpringBootApplication:

This is a combined annotation used to mark the main startup class. It is a combination of @Configuration, @EnableAutoConfiguration and @ComponentScan, used to enable Spring Boot’s automatic configuration and component scanning.

@Component:

Marking a class as a component is usually used to mark ordinary Spring-managed bean classes.

@Controller:

Mark a class as a controller (Controller) for handling HTTP requests.

@RestController:

Similar to @Controller, but with @ResponseBody annotation, indicating that RESTful style data is returned.

@Service:

Mark a class as a service layer component, which is generally used to define business logic.

@Repository:

Mark a class as a persistence layer (Repository) component for database operations.

@RequestMapping:

Mark a method or class as a handler for request mapping, used to handle HTTP requests and return responses.

@Autowired:

Autowiring (dependency injection) annotations are used to inject beans in the Spring container into specified classes or fields.

@Value:

Used to inject values ​​from configuration files into Spring-managed beans.

@PostConstruct:

Mark a method to be executed after bean initialization is complete.

@PreDestroy:

Mark a method to be executed before the bean is destroyed.

@Conditional:

Used to decide whether to instantiate a bean based on conditions.

@EnableAutoConfiguration:

Enable automatic configuration and let Spring Boot automatically configure the project based on the dependencies on the classpath.

@RequestBody:

Bind the content of the HTTP request to the annotated method parameters.

@PathVariable:

Used to get parameter values ​​from URL.

@Configuration:

Mark a class as a configuration class to define some beans or configuration items.

@Import:

Used to import beans from other configuration classes into the current configuration class.

@Profile:

Used to specify that the current configuration class or bean is only activated in a specific environment (such as dev, test, prod, etc.).

@Value:

Used to inject values ​​from a properties file into class properties.

Guess you like

Origin blog.csdn.net/qq_63499305/article/details/132850261