最近对单点系统进行微服务拆分,被各个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