最近對單點系統進行微服務拆分,被各個springboot的組件註冊搞得雲裏霧裏的。(有的是經過springboot的自動配置進IOC容器的,有的是本身添加構造方法添加進IOC容器。)決定抽時間將spring註解掃描組件註冊從新複習一下,很久沒寫博客了,也該用筆記記錄一下本身的學習過程,再不清晰的時候回來看一下加深印象。html
1、@Configuration和@Bean給容器註冊組件java
如今咱們有以下一個bean,想要將其注入到IOC容器中:web
package com.kun.bean; import org.springframework.beans.factory.annotation.Value; public class Person {
private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age "]"; } }
傳統經過配置文件方式定義bean的方式以下:spring
<bean id="person" class="com.kun.bean.Person"> <property name="age" value="${}"></property> <property name="name" value="zhangsan"></property> </bean>
如今咱們經過一個config類來替代原來的配置文件:數據庫
package com.kun.config; import com.kun.bean.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScans; import org.springframework.context.annotation.Configuration; @Configuration public class MainConfig { @Bean({"person"}) public Person person01() { return new Person("lisi", Integer.valueOf(20)); } }
這樣,咱們就將Person的實例注入到了IOC容器中,測試以下:express
package com.kun; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kun.bean.Person; import com.kun.config.MainConfig; public class MainTest { @SuppressWarnings("resource") public static void main(String[] args) { // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); // Person bean = (Person) applicationContext.getBean("person"); // System.out.println(bean); ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); Person bean = applicationContext.getBean(Person.class); System.out.println(bean); String[] namesForType = applicationContext.getBeanNamesForType(Person.class); for (String name : namesForType) { System.out.println(name); } } }
最終咱們可以從IOC容器中獲取到注入的Person對象。apache
注意:經過@Configuration和@Bean註冊的對象,在IOC容器中的key默認爲構造方法的方法名,若是想要改變,則給@Bean註解增長value屬性,IOC容器中的key可改成value屬性的值。編程
2、@ComponentScan自動掃描組件bootstrap
傳統經過配置文件方式定義bean的方式以下:windows
<!-- 包掃描、只要標註了@Controller、@Service、@Repository,@Component --> <context:component-scan base-package="com.kun" default-filters="false"></context:component-scan>
如今咱們經過一個config類來替代原來的配置文件中的配置:
package com.kun.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScans; import com.kun.bean.Person; import org.springframework.core.annotation.Order; //配置類==配置文件 @Configuration //告訴Spring這是一個配置類 @Order(2) @ComponentScans( value = { @ComponentScan(value= "com.kun",includeFilters = { /* @Filter(type=FilterType.ANNOTATION,classes={Controller.class}), @Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/ @Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class}) },useDefaultFilters = false) } ) //@ComponentScan value:指定要掃描的包 //excludeFilters = Filter[] :指定掃描的時候按照什麼規則排除那些組件 //includeFilters = Filter[] :指定掃描的時候只須要包含哪些組件 //FilterType.ANNOTATION:按照註解 //FilterType.ASSIGNABLE_TYPE:按照給定的類型; //FilterType.ASPECTJ:使用ASPECTJ表達式 //FilterType.REGEX:使用正則指定 //FilterType.CUSTOM:使用自定義規則 public class MainConfig { //給容器中註冊一個Bean;類型爲返回值的類型,id默認是用方法名做爲id @Bean("person") public Person person01(){ return new Person("lisi", 20); } }
@ComponentScan註解是一個數組,其中數組中的元素以下:
/* * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.core.type.filter.TypeFilter; /** * Configures component scanning directives for use with @{@link Configuration} classes. * Provides support parallel with Spring XML's {@code <context:component-scan>} element. * * <p>One of {@link #basePackageClasses()}, {@link #basePackages()} or its alias * {@link #value()} may be specified to define specific packages to scan. If specific * packages are not defined scanning will occur from the package of the * class with this annotation. * * <p>Note that the {@code <context:component-scan>} element has an * {@code annotation-config} attribute, however this annotation does not. This is because * in almost all cases when using {@code @ComponentScan}, default annotation config * processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore, * when using {@link AnnotationConfigApplicationContext}, annotation config processors are * always registered, meaning that any attempt to disable them at the * {@code @ComponentScan} level would be ignored. * * <p>See @{@link Configuration}'s javadoc for usage examples. * * @author Chris Beams * @since 3.1 * @see Configuration */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface ComponentScan { /** * Alias for the {@link #basePackages()} attribute. * Allows for more concise annotation declarations e.g.: * {@code @ComponentScan("org.my.pkg")} instead of * {@code @ComponentScan(basePackages="org.my.pkg")}. */ String[] value() default {}; /** * Base packages to scan for annotated components. * <p>{@link #value()} is an alias for (and mutually exclusive with) this attribute. * <p>Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names. */ String[] basePackages() default {}; /** * Type-safe alternative to {@link #basePackages()} for specifying the packages * to scan for annotated components. The package of each class specified will be scanned. * <p>Consider creating a special no-op marker class or interface in each package * that serves no purpose other than being referenced by this attribute. */ Class<?>[] basePackageClasses() default {}; /** * The {@link BeanNameGenerator} class to be used for naming detected components * within the Spring container. * <p>The default value of the {@link BeanNameGenerator} interface itself indicates * that the scanner used to process this {@code @ComponentScan} annotation should * use its inherited bean name generator, e.g. the default * {@link AnnotationBeanNameGenerator} or any custom instance supplied to the * application context at bootstrap time. * @see AnnotationConfigApplicationContext#setBeanNameGenerator(BeanNameGenerator) */ Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; /** * The {@link ScopeMetadataResolver} to be used for resolving the scope of detected components. */ Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class; /** * Indicates whether proxies should be generated for detected components, which may be * necessary when using scopes in a proxy-style fashion. * <p>The default is defer to the default behavior of the component scanner used to * execute the actual scan. * <p>Note that setting this attribute overrides any value set for {@link #scopeResolver()}. * @see ClassPathBeanDefinitionScanner#setScopedProxyMode(ScopedProxyMode) */ ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT; /** * Controls the class files eligible for component detection. * <p>Consider use of {@link #includeFilters()} and {@link #excludeFilters()} * for a more flexible approach. */ String resourcePattern() default ClassPathScanningCandidateComponentProvider.DEFAULT_RESOURCE_PATTERN; /** * Indicates whether automatic detection of classes annotated with {@code @Component} * {@code @Repository}, {@code @Service}, or {@code @Controller} should be enabled. */ boolean useDefaultFilters() default true; /** * Specifies which types are eligible for component scanning. * <p>Further narrows the set of candidate components from everything in * {@link #basePackages()} to everything in the base packages that matches * the given filter or filters. * @see #resourcePattern() */ Filter[] includeFilters() default {}; /** * Specifies which types are not eligible for component scanning. * @see #resourcePattern() */ Filter[] excludeFilters() default {}; /** * Declares the type filter to be used as an {@linkplain ComponentScan#includeFilters() * include filter} or {@linkplain ComponentScan#excludeFilters() exclude filter}. */ @Retention(RetentionPolicy.RUNTIME) @Target({}) @interface Filter { /** * The type of filter to use. Default is {@link FilterType#ANNOTATION}. */ FilterType type() default FilterType.ANNOTATION; /** * The class or classes to use as the filter. In the case of * {@link FilterType#ANNOTATION}, the class will be the annotation itself. * In the case of {@link FilterType#ASSIGNABLE_TYPE}, the class will be the * type that detected components should be assignable to. And in the case * of {@link FilterType#CUSTOM}, the class will be an implementation of * {@link TypeFilter}. * <p>When multiple classes are specified, OR logic is applied, e.g. "include * types annotated with {@code @Foo} OR {@code @Bar}". * <p>Specifying zero classes is permitted but will have no effect on component * scanning. */ Class<?>[] value(); } }
經常使用的屬性字段有:
value:指定要掃描的包
basePackages:同value
excludeFilters = Filter[] :指定掃描的時候按照什麼規則排除那些組件
includeFilters = Filter[] :指定掃描的時候只須要包含哪些組件
Filter經常使用的枚舉以下:
FilterType.ANNOTATION:按照註解 FilterType.ASSIGNABLE_TYPE:按照給定的類型; FilterType.ASPECTJ:使用ASPECTJ表達式 FilterType.REGEX:使用正則指定 FilterType.CUSTOM:使用自定義規則
着重看了一下自定義的過濾規則,須要實現TypeFilter接口,複寫完match方法,根據方法的返回值來判斷是否掃描進IOC容器。
package com.kun.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 MyTypeFilter implements TypeFilter { /** * metadataReader:讀取到的當前正在掃描的類的信息 * metadataReaderFactory:能夠獲取到其餘任何類信息的 */ @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { // TODO Auto-generated method stub //獲取當前類註解的信息 AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata(); //獲取當前正在掃描的類的類信息 ClassMetadata classMetadata = metadataReader.getClassMetadata(); //獲取當前類資源(類的路徑) Resource resource = metadataReader.getResource(); String className = classMetadata.getClassName(); System.out.println("--->"+className); if(className.contains("er")){ return true; } return false; } }
3、@Scope設置組件的做用域
package com.kun.config; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Lazy; import com.kun.bean.Color; import com.kun.bean.ColorFactoryBean; import com.kun.bean.Person; import com.kun.bean.Red; import com.kun.condition.LinuxCondition; import com.kun.condition.MyImportBeanDefinitionRegistrar; import com.kun.condition.MyImportSelector; import com.kun.condition.WindowsCondition; //類中組件統一設置。知足當前條件,這個類中配置的全部bean註冊才能生效; @Conditional({WindowsCondition.class}) @Configuration @Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class}) //@Import導入組件,id默認是組件的全類名 public class MainConfig2 { //默認是單實例的 /** * ConfigurableBeanFactory#SCOPE_PROTOTYPE * @see ConfigurableBeanFactory#SCOPE_SINGLETON * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST request * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION sesssion * @return\ * @Scope:調整做用域 * prototype:多實例的:ioc容器啓動並不會去調用方法建立對象放在容器中。 * 每次獲取的時候纔會調用方法建立對象; * singleton:單實例的(默認值):ioc容器啓動會調用方法建立對象放到ioc容器中。 * 之後每次獲取就是直接從容器(map.get())中拿, * request:同一次請求建立一個實例 * session:同一個session建立一個實例 * * 懶加載: * 單實例bean:默認在容器啓動的時候建立對象; * 懶加載:容器啓動不建立對象。第一次使用(獲取)Bean建立對象,並初始化; * */ // @Scope("prototype") @Bean("person") public Person person(){ System.out.println("給容器中添加Person...."); return new Person("張三", 25); } }
關於@Scope註解沒啥可說的,多一句嘴,只有無狀態的Bean才能夠在多線程環境下共享,在Spring中,絕大部分Bean均可以聲明爲singleton做用域。
那麼對於有狀態的bean呢?Spring對一些(如RequestContextHolder、TransactionSynchronizationManager、LocaleContextHolder等)中非線程安全狀態的bean採用ThreadLocal進行處理,讓它們也成爲線程安全的狀態,所以有狀態的Bean就能夠在多線程中共享了。默認Contrller也是單例模式,若是出現線程安全問題須要考慮controller中是否有全局的成員變量被多線程共享,或者將其改爲多例模式。
4、@Lazy設置單例bean的懶加載
默認的多例bean在IOC容器啓動時並不建立,只有在第一次使用(獲取)Bean建立對象的時候新生成一個,若是想要單例bean也實現懶加載的策略,此時則須要@Lazy註解。
@Lazy @Bean("person") public Person person(){ System.out.println("給容器中添加Person...."); return new Person("張三", 25); }
5、@Conditional按照條件註冊bean
@Conditional註解對類中組件統一設置。知足當前條件,這個類中配置的全部bean註冊才能生效。該註解的value爲自定義的條件類,該類實現
org.springframework.context.annotation.condition接口,複寫其中的matches方法,根據方法的返回值來判斷條件是否生效,以決定配置類是否生效。
package com.kun.config; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import com.kun.condition.WindowsCondition; //類中組件統一設置。知足當前條件,這個類中配置的全部bean註冊才能生效; @Conditional({WindowsCondition.class}) @Configuration public class MainConfig2 { }
自定義的WindowsCondition類以下:
package com.kun.condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; //判斷是否windows系統 public class WindowsCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); String property = environment.getProperty("os.name"); if(property.contains("Windows")){ return true; } return false; } }
6、@Import快速的給IOC容器註冊一個組件
package com.kun.config; import org.springframework.context.annotation.Configuration; import com.kun.bean.Color; import com.kun.bean.ColorFactoryBean; import com.kun.bean.Person; import com.kun.bean.Red; import com.kun.condition.LinuxCondition; import com.kun.condition.MyImportBeanDefinitionRegistrar; import com.kun.condition.MyImportSelector; import com.kun.condition.WindowsCondition; @Configuration @Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class}) //@Import導入組件,id默認是組件的全類名 public class MainConfig2 { }
另外,咱們能夠實現ImportSelector或者ImportBeanDefinitionRegistrar接口複寫相應的方法註冊指定的bean:
package com.kun.condition; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; //自定義邏輯返回須要導入的組件 public class MyImportSelector implements ImportSelector { //返回值,就是到導入到容器中的組件全類名 //AnnotationMetadata:當前標註@Import註解的類的全部註解信息 @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { // TODO Auto-generated method stub //importingClassMetadata //方法不要返回null值 return new String[]{"com.kun.bean.Blue","com.kun.bean.Yellow"}; } }
package com.kun.condition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; import com.kun.bean.RainBow; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { /** * AnnotationMetadata:當前類的註解信息 * BeanDefinitionRegistry:BeanDefinition註冊類; * 把全部須要添加到容器中的bean;調用 * BeanDefinitionRegistry.registerBeanDefinition手工註冊進來 */ @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { boolean definition = registry.containsBeanDefinition("com.kun.bean.Red"); boolean definition2 = registry.containsBeanDefinition("com.kun.bean.Blue"); if(definition && definition2){ //指定Bean定義信息;(Bean的類型,Bean。。。) RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class); //註冊一個Bean,指定bean名 registry.registerBeanDefinition("rainBow", beanDefinition); } } }
這兩種方法任選其一便可,若是讀過spring源碼,那麼必定對RootBeanDefinition類很熟悉,Spring經過BeanDefinition將配置文件中的配置信息轉換爲容器的內部表示,並將這些BeanDefiniton註冊到BeanDefinitonRegistry中。Spring容器的BeanDefinitionRegistry就像是Spring配置信息的內存數據庫,主要是以map的形式保存,後續操做直接從BeanDefinitionRegistry中讀取配置信息。通常狀況下,BeanDefinition只在容器啓動時加載並解析,除非容器刷新或重啓,這些信息不會發生變化,固然若是用戶有特殊的需求,也能夠經過編程的方式在運行期調整BeanDefinition的定義。所以,第二種方法更好理解一些。
最後別忘了經過@Import註解將咱們自定義的選擇器添加到配置類中,@Import({MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})。
7、經過BeanFactory註冊組件
實現org.springframework.beans.factory.FactoryBean接口,自定義一個BeanFactory。getObject()中返回的對象會被註冊到IOC容器中。
package com.kun.bean; import org.springframework.beans.factory.FactoryBean; //建立一個Spring定義的FactoryBean public class ColorFactoryBean implements FactoryBean<Color> { //返回一個Color對象,這個對象會添加到容器中 @Override public Color getObject() throws Exception { // TODO Auto-generated method stub System.out.println("ColorFactoryBean...getObject..."); return new Color(); } @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return Color.class; } //是單例? //true:這個bean是單實例,在容器中保存一份 //false:多實例,每次獲取都會建立一個新的bean; @Override public boolean isSingleton() { // TODO Auto-generated method stub return false; } }
再經過@Bean註解將自定義的FactoryBean註冊到IOC容器中。
package com.kun.config; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.kun.bean.Color; import com.kun.bean.ColorFactoryBean; import com.kun.bean.Person; import com.kun.bean.Red; @Configuration public class MainConfig2 { @Bean public ColorFactoryBean colorFactoryBean(){ return new ColorFactoryBean(); } }
注意:默認獲取到的是工廠bean調用getObject建立的對象,要獲取工廠Bean自己,咱們須要給id前面加一個& &colorFactoryBean
8、小結
給容器中註冊組件
1)、包掃描+組件標註註解(@Controller/@Service/@Repository/@Component)[本身寫的類]
2)、@Bean[導入的第三方包裏面的組件]
3)、@Import[快速給容器中導入一個組件]
1)、@Import(要導入到容器中的組件);容器中就會自動註冊這個組件,id默認是全類名
2)、ImportSelector:返回須要導入的組件的全類名數組;
3)、ImportBeanDefinitionRegistrar:手動註冊bean到容器中
4)、使用Spring提供的 FactoryBean(工廠Bean)
1)、默認獲取到的是工廠bean調用getObject建立的對象
2)、要獲取工廠Bean自己,咱們須要給id前面加一個& &colorFactoryBean