Common annotations in SpringBoot

Some annotations that are frequently asked in interviews and commonly used in actual development

@Bean is often used in configuration classes, such as the startup class in SpringBoot. The startup class itself is also a configuration class, or @Configuration (configuration class). The function of the annotation is to declare the return value of a method as a Spring Bean, and Spring will The return value of this method is added to the Spring container at runtime. The default method name , note: if the method name is capitalized, then the reference of the object in the Spring container will also be capitalized

You can also specify the reference name of the bean through @Bean("xxx")

@Component: Common annotations for Spring container management

@Controller: Let the Spring container manage and represent the control layer

@RestController: Equivalent to @ResponseBody+@Controller, indicating that this class is handed over to Spring management, and this class is the control layer, returning the data format specified by the front end, usually JSON data

@Service: Let the Spring container manage and represent the business layer

@Mapper: Annotation of the Mybatis framework, the class annotated by @Mapper will be scanned by Mybatis to generate the corresponding Mapper proxy object

Note here:

When using SpringBoot and Mybatis, both @Mapper and @Repository can be used to mark the persistence layer interface. The main differences are:

@Mapper is an annotation provided by the Mybatis framework, which is used to mark the interface of the persistence layer, and the proxy object of the interface is generated by the Mybatis framework through MapperProxyFactory. The proxy object will automatically implement the interface, and when executing the interface method, it will call the underlying SqlSession of Mybatis to execute the corresponding SQL statement.

@Repository is an annotation provided by the Spring framework for marking data access layer (persistence layer) components. If the @Repository annotation is used for the persistence layer interface, the proxy object of the interface will be created by the Spring framework through the JDK dynamic proxy. Spring will dynamically generate a proxy class according to the interface definition, and delegate the call to the interface method to SqlSessionTemplate to execute the corresponding SQL statement.

I always remember that there was an error reporting problem in the controller using custom AOP in the demo

The result is this reason

AOP in Spring 5.x still uses JDK dynamic proxy by default

Starting from SpringBoot 2.x, CGLIB is used to solve the type conversion exceptions that may be caused by using JDK dynamic proxy.

In SpringBoot 2.x, if you need to replace the JDK dynamic proxy, you can modify it through the configuration item spring.aop.proxy-target-class=false. The proxyTargetClass configuration is invalid. SpringBoot uses CGLIB by default.

JDK Proxy:

        Requirements: The target object must implement the interface

        The proxy object must also implement the interface of the target object

        Target object/agent relationship: target object and proxy object sibling relationship

CGLIB Proxy:

        Requirement: Regardless of whether the target object has an interface, a proxy object can be created for it

        Proxy requirements: the proxy object must inherit the target object

        Target object/proxy relationship: target object and proxy object are parent-child relationship

The performance of the dynamic proxy object created by CGLib is much higher than that of the JDK dynamic proxy during actual operation. Some studies have shown that it is about 10 times higher;

However, CGLib takes much more time to create objects than JDK dynamic proxy. Some studies have shown that there is about an 8-fold gap;
 

There is a limitation of JDK proxy, that is, the target object using dynamic proxy must implement one or more interfaces. If you want to proxy a class that does not implement the interface, you can use CGLib proxy

CGLib proxies can extend Java classes and implement Java interfaces at runtime. Widely used in AOP frameworks

Notes on Spring AOP The Spring used in the following two pictures was not SpringBoot

Using AOP in SpringBoot only requires aspect @Aspect

 @Aspect means it is an aspect

@EnableScheduling: Used to start a class or configure a class to start a scheduled task

@Scheduled: Turn on the scheduled task, pay attention to understand the cron expression.

@EnableAsync: Support for enabling asynchronous tasks in startup classes or configuration classes

@Async: Used to mark a method as a task executed asynchronously

@ControllerAdvice: Used to define and handle global exception handlers in SpringBoot

@ResponseBody: Return the data format specified by the front end in the body of the response

@Autowired: Annotation of the Spring framework - automatic injection

@Resource: J2EE annotations , also used for automatic injection

Some differences:

@Autowired defaults to autowiring by type (byType) (find it yourself in more detail, don't remember...)

@Resource is automatically assembled by name (byName) by default

When using the @Autowired or @Resource annotation, when there are multiple beans of the same type in the container, ambiguity will arise. In this case, you need to use the @Qualifier annotation to specify the specific Bean object to be injected. The @Qualifier annotation can be combined with @Autowired or @Resource to specify the name of the Bean to be injected

The @Value annotation is generally used to automatically inject application.properties or yaml yml files into a class managed by Spring. @Value("${my.custom.property}") can also be read by ${java.home} Get system properties

Note that even if the class is not managed by Spring, the @Value annotation will be automatically assigned when the class is instantiated

@ImportResource is generally used together with the configuration class @Configuration: the annotation is used to import the beans defined in the external XML configuration file into the Spring configuration. It is used to load Spring Bean definitions in external XML files and register these Bean definitions in the Spring IOC container

@ConfigurationProperties: Generally used together with @Configuration to bind properties to a configuration class and specify a prefix through prefix (what word to use, I can’t think of it)

@PropertySource: Used to specify the location of the property file and load the content of the property file into the Spring environment. By default, only files in the .properties suffix format can be read. It can be read through PropertySourceFactory. yml .yaml file

It is necessary to customize a class to inherit the PropertySourceFactory rewriting method. When using this annotation, specify the factory

An example of using @ConfigurationProperties, @Configuration, @PropertySource together

SpringBoot reads the List collection configured in properties_springboot properties list_biubiubiu0706's Blog-CSDN Blog

Transaction annotation: @Transactional

DataSourceTransactionManager

When using the @Transactional annotation in Spring Boot, there is no need to add the @EnableTransactionManager annotation to the startup class, because the transaction manager has been automatically integrated in starters such as spring-boot-starter-data-jpa and spring-boot-starter-jdbc (transaction manager) and configured, you can directly use the @Transactional annotation.

If you are not using the starter provided by Spring Boot, or you manually configure the transaction manager, you need to add the @EnableTransactionManager annotation to the startup class.

Guess you like

Origin blog.csdn.net/tiantiantbtb/article/details/129819032