In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the core annotation of Spring Boot". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the core annotation of Spring Boot".
Configuration
After Spring4, it is officially recommended to use Java Config instead of Application.xml declaration to hand over Bean to container management. In Spring Boot, Java Config uses completely instead of application.xml to implement the zero configuration of xml. Open the following example
Example
Create a bean class
Public class SomeBean {public void doWork () {System.out.println ("do work...");}}
Where dowork is the logical method to create another Config class
@ Configuration public class Config {@ Bean public SomeBean someBean () {return new SomeBean ();}}
Here, an @ configuration annotation is added to the Config class, which can be understood as the configuration class in Spring. The return value of the @ bean annotation is also added to the someBean,someBean method, and the returned object will be managed by the Spring container.
Simple test
Public class Test {public static void main (String [] args) {ApplicationContext context = new AnnotationConfigApplicationContext (Config.class); SomeBean sb = context.getBean (SomeBean.class); sb.doWork ();}}
Here, you create an AnnotationConfigApplicationContext object, and when you pass in Config.class, you get the someBean object.
Do work...
Expansion
In general, a complete bean needs to include id,class,initMethod,destroyMethod, ref,scope. So Java Config is used here to configure these properties. Modify the first example code
Public class SomeBean {private void init () {System.out.println ("init...");} public void doWork () {System.out.println ("do work...");} private void destroy () {System.out.println ("destroy...");}}
Add, init,destroy method
Configuration public class Config {@ Bean (initMethod = "init", destroyMethod = "destroy") public SomeBean someBean () {return new SomeBean ()}}
On the bean annotation, the attribute points to the corresponding method.
Public class Test {public static void main (String [] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (Config.class); SomeBean sb1 = context.getBean (SomeBean.class); System.out.println (sb1); SomeBean sb2 = context.getBean (SomeBean.class); System.out.println (sb2); context.close ();}}
The output is
Init... Com.spring.SomeBean@16022d9d com.spring.SomeBean@16022d9d destroy...
This completes a configuration lifecycle.
@ ComponentScan
@ ComponentScan annotation is used for the main scan path specified on the class or interface. Spring will automatically assemble the class with the specified annotation under the specified path into the bean container, including @ Controller,@Service,@Component,@Repository and so on. Its effect is equivalent to that of
Configuration.
Basic use
The commonly used attribute is basePackages,value, which specifies the scan path, and if empty, the package scan path where the class annotated with @ ComponentScan is located. BasePackageClasses: specify the class includeFilters to scan specifically: specify the class excludeFilters that meets the Filter condition: specify the classes includeFilters and excludeFilters that exclude the Filter condition. Optional: ANNOTATION= annotation type default, ASSIGNABLE_TYPE (specify fixed class), ASPECTJ (ASPECTJ type), REGEX (regular expression), CUSTOM (custom type). Common configurations for custom Filter that implement the TypeFilter API @ ComponentScan are as follows:
@ ComponentScan (value= "com.maple.learn", excludeFilters = {@ ComponentScan.Filter (type= FilterType.CUSTOM, classes= TypeExcludeFilter.class)}, includeFilters = {@ ComponentScan.Filter (type=FilterType.ANNOTATION,classes= {Controller.class})}) public class SampleClass {. }
@ EnableAutoConfiguration
The annotation is a combined annotation, and its source code is as follows
@ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented @ Inherited @ AutoConfigurationPackage @ Import (AutoConfigurationImportSelector.class) public @ interface EnableAutoConfiguration {String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; Class [] exclude () default {}; String [] excludeName () default {};}
The most important of these is the @ Import (AutoConfigurationImportSelector.class) annotation, which helps Spring Boot applications load all eligible @ Configuration configurations into the IOC container with AutoConfigurationImportSelector,@EnableAutoConfiguration, and the most important thing is to load the META-INF/spring.factories configuration with the help of a utility class of the Spring framework. The spring.factories file is a typical properties configuration file in the form of key=value, but key and value are complete class names, for example.
Org.springframework.data.repository.core.support.RepositoryFactorySupport=org.springframework.data.jpa.repository.support.JpaRepositoryFactory thank you for reading, the above is the content of "what is the core annotation of Spring Boot". After the study of this article, I believe you have a deeper understanding of what the core annotation of Spring Boot is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.