In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the 20 common Spring annotations that Java developers need to master? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Annotations themselves have no function, just like xml. Annotations and xml are a kind of metadata, which is the data that interprets the data, which is called configuration.
The following is a brief introduction to the comments related to Spring | Spring MVC.
Spring part
1. Comments on declaring bean
@ Component component, no clear role
@ Service is used in the business logic layer (service layer)
@ Repository is used in the data access layer (dao layer)
@ Controller is used in the presentation layer, declaration of the controller (C)
2. Comments on injecting bean
@ Autowired: provided by Spring
@ Inject: provided by JSR-330
@ Resource: provided by JSR-250
Can be annotated on set methods and properties, it is recommended to comment on attributes (at a glance, write less code).
3. Notes on java configuration class
@ Configuration declares that the current class is a configuration class, which is equivalent to a Spring configuration in the form of xml (on the class)
@ Bean comment on the method, declaring that the return value of the current method is a bean, replacing the way in xml (method)
@ Configuration declares that the current class is a configuration class, and the @ Component annotation is combined internally, indicating that the class is a bean (on the class)
@ ComponentScan is used to scan Component, which is equivalent to that in xml (on class)
@ WishlyConfiguration is a combination of @ Configuration and @ ComponentScan annotations, which can be replaced by these two annotations
4. Notes on AOP
Spring supports annotated facet programming for AspectJ.
@ Aspect declares an aspect (on class)
Use @ After, @ Before, @ Around to define advice, which can directly take the interception rule (pointcut) as a parameter.
@ After executes after method execution (on method)
@ Before executes before method execution (on method)
@ Around executes before and after method execution (on methods)
@ PointCut declares pointcut
Use the @ EnableAspectJAutoProxy annotation in the java configuration class to enable Spring support for AspectJ agents (on the class)
5. @ Bean attribute support
@ Scope sets how the Spring container creates a new Bean instance (@ Bean is required on the method)
The types of settings include:
Singleton (singleton, there is only one bean instance in a Spring container, default mode)
Protetype (create a new bean for each call)
Request (in the web project, create a new bean for each http request)
Session (in the web project, create a new bean for each http session)
GlobalSession (create a new Bean instance for each global http session)
@ StepScope is also involved in Spring Batch
@ PostConstruct is provided by JSR-250 and executed after the constructor is executed, which is equivalent to the initMethod of bean in the xml configuration file
@ PreDestory is provided by JSR-250 and executed before Bean is destroyed, which is equivalent to the destroyMethod of bean in xml configuration file
6. @ Value comments
@ Value injects the value for the attribute (on the attribute)
Support injection in the following ways:
"inject ordinary characters
"injecting operating system properties
"inject expression results
"inject other bean attributes
"inject file resources
"inject website resources
"injection profile
How to use injection configuration:
① Writing configuration File (test.properties)
Book.name= trisomy
② @ PropertySource load configuration file (on class)
③ also needs to configure a bean for PropertySourcesPlaceholderConfigurer.
7. Environment switching
Profile sets the configuration environment that the current context needs to use by setting the ActiveProfiles of the Environment. (on a class or method)
You can use this annotation in @ Conditional Spring4 to define the bean of a conditional conversation and determine whether the bean is instantiated by implementing the Condition interface and overriding the matches method. (method)
8. Asynchronous correlation
@ EnableAsync configuration class, use this annotation to enable support for asynchronous tasks, narrative AsyncConfigurer interface (above)
@ Async uses this annotation in the actual bean method to declare that it is an asynchronous task (all methods on the method or class will be asynchronous, requiring @ EnableAsync to start the asynchronous task)
9. Timed task related
@ EnableScheduling is used on the configuration class to enable support for scheduled tasks (on the class)
@ Scheduled to declare that this is a task, including cron,fixDelay,fixRate and other types (in method, you need to enable support for scheduled tasks)
10. @ Enable* Note
These comments are mainly used to turn on support for xxx.
@ EnableAspectJAutoProxy enables support for AspectJ automatic agents
@ EnableAsync enables support for asynchronous methods
@ EnableScheduling enables support for scheduled tasks
@ EnableWebMvc enables configuration support for Web MVC
@ EnableConfigurationProperties enables support for @ ConfigurationProperties annotation configuration Bean
@ EnableJpaRepositories enables support for SpringData JPA Repository
@ EnableTransactionManagement enables support for annotated transactions
@ EnableTransactionManagement enables support for annotated transactions
@ EnableCaching enables annotated cache support
11. Test-related notes
@ RunWith runner, which is commonly used in Spring to support JUnit
@ ContextConfiguration is used to load the configuration ApplicationContext, where the classes property is used to load the configuration class
SpringMVC part
@ EnableWebMvc enables Web MVC configuration support in the configuration class, such as some ViewResolver or MessageConverter, etc. If this sentence is not available, override the WebMvcConfigurerAdapter method (for SpringMVC configuration).
@ Controller declares the class as Controller in SpringMVC
@ RequestMapping is used to map Web requests, including access paths and parameters (on classes or methods)
@ ResponseBody supports putting the return value in the response instead of a page, and usually the user returns json data (next to the return value or on the method)
@ RequestBody allows the parameters of request to be in the request body, rather than connecting directly to the address. (put in front of parameters)
@ PathVariable is used to receive path parameters, such as the path declared by @ RequestMapping ("/ hello/ {name}"). You can get this value by placing comments in front of the parameters, usually as an interface implementation of Restful.
@ RestController this annotation is a combined annotation, equivalent to the combination of @ Controller and @ ResponseBody, and annotated on the class, meaning that @ ResponseBody is added to all methods of the Controller by default.
@ ControllerAdvice through this annotation, we can place the global configuration of the controller in the same place, and the method of the class annotated with @ Controller can be annotated to the method using @ ExceptionHandler, @ InitBinder, @ ModelAttribute
This works for all methods within the controller annotated with @ RequestMapping.
@ ExceptionHandler is used to globally handle exceptions in the controller
@ InitBinder is used to set WebDataBinder,WebDataBinder to automatically bind foreground request parameters to Model.
The original function of @ ModelAttribute is to bind the key-value pair to the Model. In @ ControllerAdvice, the global @ RequestMapping can get the key-value pair set here.
The answers to the 20 common Spring notes that Java developers need to master are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.