Spring Annotation Encyclopedia parsing spring springboot annotations, etc.

Source: Internet
Author: User
today I've sorted out the annotations that spring uses: I hope I can help you!
@ServiceFor labeling business layer components @ControllerUsed to label the control layer component (such as action in struts) @RepositoryUsed to annotate a data access component, the DAO component @ComponentAs a component, we can use this annotation to annotate when the component is not properly categorized. @AutowiredThe getter () and setter () methods are not required, and spring is automatically injected. Mark the front of the interface @AutowiredAnnotations allow an interface to be injected into a container, such as @Autowired @Qualifier ("Chinese") PrivateMan Mans; When the interface has two implementation classes must use @Qualifier specify which implementation class to inject, otherwise you can omit, write only @autowired. Use @Autowired TimeYour organdaoibatis must be @service or @component annotations.
Before the user was using 3 annotations to annotate their main class. respectively is @Configuration, @EnableAutoConfiguration, @ComponentScan。 Since these annotations are generally used together, spring boot provides a unified annotation @SpringBootApplication@SpringBootApplication= (default property) @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication Public classApplicationmain { Public Static voidMain (string[] args) {Springapplication.run (application. class, args); }   } equal to:@Configuration @EnableAutoConfiguration @ComponentScan Public classApplicationmain { Public Static voidMain (string[] args) {Springapplication.run (application. class, args); }   }
Explain @configuration, @EnableAutoConfiguration, @ComponentScan separately. 1, @Configuration: Mention of @configuration will mention his partner @bean. Using these two annotations, you can create a simple spring configuration class that can be used to replace the corresponding XML configuration file. Derive from my Code slice <beans> <beanid = "Car" class= "Com.test.Car" > <propertyName= "wheel" ref = "Wheel" ></property> </bean> <beanid = "Wheel" class= "Com.test.Wheel" ></bean> </beans>
Equivalent to: derived from my Code slice @Configuration Public classConf {@Bean PublicCar car () {Car car = NewCar (); Car.setwheel (Wheel ()); returnCar } @Bean PublicWheel Wheel () { return NewWheel (); The annotation class @Configuration to identify this class can use the spring IOC container as the source for the bean definition. @Bean annotation tells Spring that an annotation method with @bean returns an object that should be registered as a bean in the context of the spring application. 2, @EnableAutoConfiguration: Can automatically configure the context of spring, trying to guess and configure the Bean class you want, usually automatically based on your classpath and your bean definition of automatic configuration. 3, @ComponentScan: will automatically scan the specified package under the @component of all the classes, and registered as a bean, of course, including @component under the sub-annotation @service, @Repository, @Controller.
@ResponseBodyA function decorated with this annotation will directly populate the HTTP response body with the result, typically used to build the RESTful API; @ControllerUsed to define a controller class in which the controller is responsible for forwarding a URL request sent by a user to the corresponding service interface (services layer) in the spring project. @RestControllerCollection of @ResponseBody and @controller @RequestMappingProvides routing information that is responsible for mapping specific functions in URLs to controller. @EnableAutoConfigurationSpring Boot automatic configuration (auto-configuration): Try to automatically configure your spring application based on the jar dependencies you add. For example, if you have hsqldb under your classpath and you have not manually configured any database connection beans, then we will automatically configure a memory (in-memory) database. You can add @enableautoconfiguration or @springbootapplication annotations to a @configuration class to select automatic configuration. If you find that you have applied specific automatic configuration classes that you do not want, you can disable them by using the exclusion attribute of the @enableautoconfiguration annotation. The example code is as follows: @ComponentScanIndicates that the class is automatically discovered (scanned) and registered as a bean, and all spring components, including the @configuration class, are automatically collected. We often use @componentscan annotations to search for beans and import with @autowired annotations. @ConfigurationEquivalent to a traditional XML configuration file, if some Third-party libraries need XML files, it is recommended that you still use the @configuration class as the configuration master for your project--you can load the XML configuration file with @importresource annotations. @SpringBootApplicationA combination of @enableautoconfiguration, @ComponentScan, and @configuration. @ImportUsed to import other configuration classes. @ImportResourceUsed to load an XML configuration file. @AutowiredAutomatic import of dependent beans @ServiceComponents that are typically used to modify the service layer @RepositoryUsing @repository annotations ensures that DAO or repositories provide exception translation, and that the DAO or repositories class decorated with this annotation is Componetscan discovered and configured without the need to provide them with XML configuration items
Use the configuration required by the MyBatis annotation. As shown in the following code, use the @MapperScanTo scan the registration MyBatis database interface class, where the Basepackages property indicates the package where the interface class resides, Sqlsessiontemplateref indicates the sqlsessiontemplate used by the interface class. If the project requires two databases to be configured, @MapperScan also need to be configured separately.
Join the QQ Exchange Group to study together: 415777345

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.