1.@Configuration annotation This is equivalent to a configuration file , That is to say spring This is a configuration class .

2.@Bean
Register a container with Bean; The type is the type of the return value ,id The default is to use the method name as the id.
@Configuration public class MainConfig { @Bean public Person per(){ return new
Person(" Li Si ",22 ); } }
This is basically equivalent to the previous one xml The configuration file is as follows :
<bean id="per" class="com.learn.entity.Person"> <property name="name" value=
" Li Si " /> <property name="age" value="22"/> </bean>
3.@ComponentScan
Automatic package scan annotation
value: Specify packages to scan
excludeFilters=Filter[] Specify which components are excluded by what rules when scanning .
includeFilters=Filter[]
Specify which components need to be included in the scan .( Note that the default for package scanning is to scan all , that is use-default-filters=”true”, So it needs to be set to false.
This is the same as the configuration file )
FilterType.ANNOTATION : According to the way of annotation
FilterType.ASSIGNABLE_TYPE: According to the given type
FilterType.CUSTOM: Use custom rules . The need to use this is TypeFilter Implementation class of
FilterType.CUSTOM use The cases are as follows :
package com.learn.config; import java.io.IOException; import
org.springframework.core.io.Resource;import
org.springframework.core.type.AnnotationMetadata;import
org.springframework.core.type.ClassMetadata;import
org.springframework.core.type.classreading.MetadataReader;import
org.springframework.core.type.classreading.MetadataReaderFactory;import
org.springframework.core.type.filter.TypeFilter;public class MyFilterCustom
implements TypeFilter { /** * metadataReader Read the information of the class currently being scanned *
metadataReaderFactory You can get information about any other class */ public boolean match(MetadataReader
metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException {
// Gets the information of the current class annotation AnnotationMetadata annotationMetadata =
metadataReader.getAnnotationMetadata();// Gets information about the class that is currently being scanned ClassMetadata
classMetadata = metadataReader.getClassMetadata();// Get current class resource ( Classpath ) Resource
resource = metadataReader.getResource(); String className =
classMetadata.getClassName(); System.out.println(" Information about the currently scanned class "+className); if
(className.contains("Controller")){ return true; } return false; } }
@Configuration @ComponentScans(value={ @ComponentScan(value="com.learn",
includeFilters={@Filter(type=FilterType.CUSTOM,classes={MyFilterCustom.class})
},useDefaultFilters=false) }) public class MainConfig { @Bean public Person per
(){return new Person(" Li Si ",22 ); } }

FilterType.ASPECTJ: use ASPECTJ expression ( Basically not )
FilterType.REGEX : Using regular expressions
@Configuration @ComponentScan(value="com.learn", includeFilters={ @Filter
(type=FilterType.ANNOTATION, classes={Controller.class} )},useDefaultFilters=
false) public class MainConfig { @Bean public Person per(){ return new Person(
" Li Si ",22 ); } }
This is basically the same as a configuration file :
<context:component-scan base-package="com.learn" use-default-filters="false"> <
context:include-filter type="annotation" expression=
"org.springframework.stereotype.Controller"/> </context:component-scan> <bean
id="per" class="com.learn.entity.Person"> <property name="name" value=" Li Si " /> <
property name="age" value="22"/> </bean>
4.@ComponentScans This is more than one scan Several sets of , In other words, multiple rules can be defined . If it is jdk1.8 Later , It can also be a few more times scan.
no need scans it's fine too
@Configuration @ComponentScans(value={ @ComponentScan(value="com.learn",
includeFilters={@Filter(type=FilterType.ANNOTATION, classes={Controller.class}
)},useDefaultFilters=false) }) public class MainConfig { @Bean public Person per
(){return new Person(" Li Si ",22 ); } }
4.@Scope Scope of action

* prototype Multi instance
remarks :IOC Container startup does not call methods to create objects in the container , Instead, the method is called to create the object each time it is retrieved , And each time a new one is created , That is, multiple instances .
* singleton Singleton ( Default value ) remarks : The default is in the case of single instance ,IOC When the container starts, the method is called to create the object , Put in iOC In the container .
Every time I get it, I take it from the container , That is, all objects are one .
* request Create an instance at the same request (web It can only be used in the environment , And basically not )
* session The same session Create an instance (web It can only be used in the environment , And basically not )
Default single column :
@Configuration public class MainConfig2 { @Bean public Person person(){ return
new Person(" Zhang San ", 28); } } ApplicationContext context = new
AnnotationConfigApplicationContext(MainConfig2.class); String[]
beanDefinitionNames = context.getBeanDefinitionNames(); for(String
beanName:beanDefinitionNames){ System.out.println(" current bean Name of :"+beanName); }
System.out.println(" There are altogether "+context.getBeanDefinitionCount()+" individual bean It's scheduled "); Object
obj1=context.getBean("person"); Object obj2=context.getBean("person"); System
.out.println("obj1,obj2:"+(obj1==obj2));

Using multiple examples :
@Configuration public class MainConfig2 { @Bean @Scope("prototype") // Multiple cases public
Personperson(){ return new Person(" Zhang San ", 28); } }

5.@Lazy Lazy loading , For single instance bean
Lazy loading : Container startup does not create objects . First use ( obtain bean) Objects created , And initialize .
@Configuration public class MainConfig2 { @Bean @Lazy public Person person(){
System.out.println(" Called person method "); return new Person(" Zhang San ", 28); } }
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2
.class); String[] beanDefinitionNames = context.getBeanDefinitionNames();
for(String beanName:beanDefinitionNames){ System.out.println(" current bean Name of :"
+beanName); } System.out.println(" There are altogether "+context.getBeanDefinitionCount()+
" individual bean It's scheduled "); System.out.println("-------IOC Container initialization completed , Start calling bean----"); Object
obj1=context.getBean("person"); Object obj2=context.getBean("person"); System
.out.println("obj1,obj2:"+(obj1==obj2));

6.@Conditional Judge according to certain conditions , Satisfy the condition to register in the container bean.
@Configuration public class MainConfig2 { @Bean("win") @Conditional
(WindowConditional.class)public Person person1(){ return new Person("Windows",
70); } @Bean("Lin") @Conditional(LinuxConditional.class) public Person person2
(){return new Person("Linux", 80); } } /*** * Is it linux Environmental Science * @author SpringLi * */
public class LinuxConditional implements Condition{ /** * ConditionContext
Determine the context in which the condition can be used * AnnotatedTypeMetadata Note information */ public boolean matches
(ConditionContext context, AnnotatedTypeMetadata metadata) {
// Can get IOC Used beanFactory ConfigurableListableBeanFactory beanFactory =
context.getBeanFactory();// Get class loader ResourceLoader resourceLoader =
context.getResourceLoader();// Get current environment information Environment environment =
context.getEnvironment();// obtain bean Defined registration class BeanDefinitionRegistry registry =
context.getRegistry(); String property = environment.getProperty("os.name"); if
(property.contains("Linux")){ return true; } return false; } } public class
WindowConditional implements Condition{ /** * ConditionContext Determine the context in which the condition can be used *
AnnotatedTypeMetadata Note information */ public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {// Can get IOC Used beanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();// Get class loader
ResourceLoader resourceLoader = context.getResourceLoader();// Get current environment information
Environment environment = context.getEnvironment();// obtain bean Defined registration class
BeanDefinitionRegistry registry = context.getRegistry(); String property =
environment.getProperty("os.name"); if(property.contains("Window")){ return true
; }return false; } } ApplicationContext context = new
AnnotationConfigApplicationContext(MainConfig2.class); String[]
beanDefinitionNames = context.getBeanDefinitionNames(); for(String
beanName:beanDefinitionNames){ System.out.println(" current bean Name of :"+beanName); }
System.out.println(" There are altogether "+context.getBeanDefinitionCount()+" individual bean It's scheduled ");

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train SparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network c++ Memory Pointer out of bounds detection mechanism _CrtMemBlockHeade Change one's mind ! Tesla starts to deliver made in China to European market Model 3Qt study 7—— Modal and modeless dialog boxes JavaScript Medium Call and ApplyJAVA Convert a string to a numeric type k8s Entry to give up --k8s Key concepts Count the number of letters (java Language implementation )