
3.2 Spring Bean的配置
3.2.1 注解配置(@Component)
当类注解为@Component、@Service、@Repository或@Controller时,Spring容器会自动扫描(通过@ComponentScan实现,Spring Boot已经做好了配置),并将它们注册成受容器管理的Bean。

@Component、@Service、@Repository和@Controller在当前示例中是完全等同的。

上面的@Component和@Service都没有给Bean命名,Spring容器会自动命名为类名的第一个字母的小写形式,即someService和someService2。一般来说,没有必要去修改Bean的名称,使用默认的Bean名即可。当然,也可以通过@Component("SomeService")来设置Bean的名称。
@Service、@Repository和@Controller这三个注解组合了@Component注解,它们是@Component语义上的特例。
◎@Component:被注解类是“组件”。
◎@Controller:被注解类是“控制器”。
◎@Service:被注解类是“服务”。
◎@Repository:被注解类是“数据仓库”。
3.2.2 Java配置(@Configuration和@Bean)
在类上注解@Configuration(@Component的特例,会被容器自动扫描),可使类成为配置类。如果使用@Bean标注在类的方法上,则方法的返回值即为Bean的实例。假如现在有另外一个类。

用Java配置的如下。

同样,没有给Bean命名。Spring会将方法名anotherService默认成Bean的名称。若需要修改,则使用@Bean(name="AnotherService")。
3.2.3 依赖注入(Dependency Injection)
1.自动注入(@Autowired)
容器已经创建了SomeService、AnotherService和SomeService2的Bean,其他的Bean应如何注入使用呢?
(1)注解注入。
AnnotationInjectionService需要使用SomeService和AnotherService的Bean,我们只需在AnnotationInjectionService构造器上注解@Autowired,即可注入参数里需要的Bean。


在构造器上注解注入是Spring推荐的注入方式,当然,也可以通过在属性上注解@Autowired来注入Bean。

还可以在set方法上注解@Autowired来注入Bean。


如果Bean只有一个构造器,则可以直接省略@Autowired注解。若Bean有多个构造器,则需注解一个构造器用来注入,示例如下。

(2)配置注入。
现在使用Java配置的方式在Bean JavaConfigInjectService中注入BeanAnotherService,JavaConfigInjectService定义如下。

前面已经将AnotherService通过@Bean注解成Bean了,下面只需在定义JavaConfigInjectService的Bean的方法参数里注入AnotherService的Bean即可。

在同一个配置类里,还可以在新建JavaConfigInjectService的构造里直接注入创建SomeService2的Bean的方法。


(3)混合注入。
注解配置的Bean可以直接注入给使用Java配置的Bean,反之亦然。
把注解配置的Bean注入Java配置的Bean:

把Java配置的Bean注入注解配置的Bean。被注入的BeanMixInjectionService2定义如下。

在JavaConfig类里,可以直接在参数中注入Bean。

2.@Primary
上面的例子都是通过Bean的名称来自动注入的。当Bean的名称不满足条件时,容器会根据Bean的类型进行自动注入。当全局只有一个类型的Bean时,自动注入是没有问题的,但是当全局有多个同类型的Bean时,会提示“required a single bean, but n were found”,此时可以通过@Primary来注解需要优先使用的Bean。假如有两个Bean:


此时有两个Bean,名称分别为anotherService和primaryAnotherService。如果在注入的地方不使用这个两个名称,那么就会按照Bean的类型自动注入。

因为现在使用的 service不符合按照名称自动注入,所以是按照类型自动注入的。因为primaryAnotherService注解了@Primary,所以使用primaryAnotherService这个Bean。
3.@Qualifier
在上面的例子中,使用UsePrimaryService注入的AnotherService的Bean只能是primaryAnotherService,这时可以使用@Qualifier直接指定需要使用哪个Bean。还是使用上面例子中的两个Bean。
注入anotherService:

注入primaryAnotherService。

3.2.4 运行检验(CommandLineRunner)
在Spring Boot下可以注册一个CommandLineRunner的Bean,在容器启动后,这个Bean可用来执行一些专门的任务,如在JavaConfig里。

a.通过参数注入当前的CommandLineRunner Bean中。
b.CommandLineRunner是一个函数接口,输入的参数为main方法里接收的args参数。这里使用Lambda表达式执行每个Bean的doMyThing方法,如图3-1所示。

图3-1
CommandLineRunner有个姊妹接口叫作ApplicationRunner,它们之间唯一的区别是ApplicationRunner使用org.springframework.boot.DefaultApplicationArguments类型的参数,示例如下。

CommandLineRunner的args是不定长字符串(String... args),而ApplicationRunner的args是DefaultApplicationArguments类型的对象。
3.2.5 Bean的Scope
容器中的Bean的Scope指的是Bean的实例在容器中创建的方式。在容器中,默认是singleton,即整个容器中只创建一个Bean的实例。常用的还有prototype,即每次请求Bean时都会创建一个实例。可以通过@Scope注解来设置Scope。
下面两种方式是相同的:


通过@Scope(BeanDefinition.SCOPE_PROTOTYPE)指定Scope为prototype:

除可以在方法上注解@Scope外,还可以在@Bean的类上注解@Scope,示例如下:

在JavaConfig中配置的代码如下:

这时可以在ScopeInjectService Bean中分别给上面三个Bean注入两次,由此判断相同类型的两个注入是否相等。


在JavaConfig中配置下面的代码。

执行效果如图3-2所示。

图3-2
3.2.6 Bean的生命周期
1.初始化和销毁
我们可以定制Bean在容器中的初始化行为和销毁行为。
(1)注解配置:使用@PostConstruct和@PreDestroy。


执行效果如图3-3所示。

图3-3
(2)Java配置:使用@Bean的initMethod和destroyMethod。
Bean的定义如下。

在JavaConfig中配置下面的代码。


执行效果如图3-4所示。

图3-4
2.延迟初始化(@Lazy)
只要在Bean上注解了@Lazy,那么Bean在被调用时就会被初始化。它可以和@Component类注解或@Bean一起使用。


因为这两个Bean没有被调用过,所以没有被初始化,此时控制台没有任何输出。
3.依赖顺序(@DependsOn)
设置Bean lifeService2依赖于lifeService,让lifeService先初始化,可以用@DependsOn来实现。

执行的结果是lifeService先于lifeService2初始化,如图3-5所示。

图3-5
3.2.7 应用环境
Spring提供了一个接口Environment来代表当前运行的应用环境,这个环境包含两部分。
◎Profile:一组命名的、定义在一起的Bean。通常为不同的应用场景(生产环境、开发环境、测试环境等)定义。
◎Property:配置属性,可以从properties文件、JVM系统属性、操作系统环境变量等外部来获得配置属性。
1.场景(@Profile)
可以通过@Profile注解指定当前的运行场景。@Profile可以和@Component、@Configuration、@Bean等一起使用,当然也分别限制了@Profile生效的Bean的分组。
下面使用需要显示不同操作系统的列表命令(在Windows下为 dir,在Linux下为 ls)的Bean。

在Windows开发环境下,场景配置如下。


在Linux开发环境下,场景配置如下。

当配置好两种不同场景下的Profile后,我们需要在应用中配置哪个是激活的Profile,手动配置如下。

因为使用了Spring Boot,所以只需在application.properties文件中做如下配置即可。

在JavaConfig里,用CommandLineRunner分别将Profile配置成production和dev,执行效果如图3-6和图3-7所示。


图3-6

图3-7
2.属性配置(@PropertySource)
Spring的Environment属性是由PropertySource组成的,我们可以通过@PropertySource指定外部配置文件的路径。这些配置文件的属性都会以PropertySource的形式注册到Environment中,@PropertySource支持XML格式和properties格式,不支持Spring Boot下的YAML格式。
现在添加2个外部配置文件。
◎author.properties:
author.name=wyf
◎book.properties:
book.name=spring boot in battle
在添加完成后,可以用一个配置类来接收这两个文件的配置。

a.当有多个外部配置时,可以用@PropertySources指定。若只有一个可用,则只使用@PropertySource("classpath:book.properties")。
b.注入Environment的Bean,因为只有一个构造器,所以可省略@Autowired。
c.可以通过@Value注解获得Environment中的属性,关于@Value的更详细的讲解见3.6节。
d.外部配置的属性都已经在Environment中注册过,可以直接获取。
3.2.8 条件配置(@Conditional)
通过@Conditional我们可以定义当满足某个特定条件(Condition)时,应该做什么配置。@Conditional同样可以和@Component、@Configuration、@Bean一起使用,进而指定条件起作用的范围。
@Conditional注解接收Condition数组作为参数,Condition即我们的特定条件。Condition只有一个方法matches,当符合条件时,返回true;当不符合条件时,返回false。
例如,判断当前系统是否是Windows的条件定义:

a.条件实现Condition接口即可。
b.matches的两个参数:ConditionContext可获得容器的相关信息;AnnotatedTypeMetadata是当前被注解的方法或类的元数据(数据的描述)信息。
c.通过容器context获得运行环境Environment信息,从而获得操作系统信息。
配置如下。

a.@Conditional使用的是OnWindowsCondition条件,只有在操作系统是Windows的情况下,当前Bean才会被创建。
在JavaConfig中使用CommandLineRunner运行。

在Windows系统才能正常执行;在非Windows系统下会报错,找不到Bean。因为不符合条件,所以没有创下这个Bean。
3.2.9 开启配置(@Enable*和@Import)
在本书后面的内容里会出现大量以@Enable*开头的注解,@Enable*会自动对相应的功能进行自动配置,如@EnableWebMvc、@EnableCaching、@EnableScheduling、@EnableAsync、@EnableWebSocket、@EnableJpaRepositories、@EnableTransactionManagement、@EnableJpaAuditing和@EnableAspectJAutoProxy等。
@Enable*的开启配置的功能依赖于@Import注解,@Import注解支持导入如下配置:
◎直接导入@Configuration配置类。
◎配置类选择器ImportSelector的实现。
◎动态注册器ImportBeanDefinitionRegistrar的实现。
◎混合以上三种。
下面将分别演示四种方式的实现。
1.直接导入@Configuration配置类
当应用注解了@Configuration后,会被Spring Boot的默认组件扫描并自动注册,所以本节的注解类代码放在 io.github.wiselyman.annotations中,配置类的代码放在io.github.wiselyman.config中。
定义注解:

定义配置类:

在JavaConfig中使用@EnableA注解,即可获得导入的配置类AConfig中的Bean a。

在JavaConfig中使用CommandLineRunner查看Bean的内容,执行结果如图3-8所示。


图3-8
2.配置类选择器ImportSelector的实现
在这个例子中,通过注解选择生效的配置类,注解定义如下。

在io.github.wiselyman.selector中定义选择器。


a.选择器要实现ImportSelector接口。
b.实现接口的selectImports方法,参数AnnotationMetadata importClassMetadata是注解使用类(本例为JavaConfig)上@EnableB的元数据信息。
c.通过@EnableB在实际使用中的元数据,获得isUppercase的值。
d.如果isUppercase==true,则此时实际使用的是@EnableB或者@EnableB(isUppercase=true),因而使用BUppercaseConfig提供的配置。
e.若实际使用的是@EnableB(isUppercase=false),则使用BLowercaseConfig提供的配置。
BUppercaseConfig的定义如下。

BLowercaseConfig的定义如下。

在JavaConfig中使用@EnableB,并用CommandLineRunner检验。

运行结果如图3-9所示。
若将isUppercase设置为false,则执行结果如图3-10所示。


图3-9

图3-10
3.动态注册器ImportBeanDefinitionRegistrar的实现
本例通过ImportBeanDefinitionRegistrar动态注册Bean到容器里。
注解定义如下:

在io.github.wiselyman.registrar中定义注册器。

a.注册器需实现ImportBeanDefinitionRegistrar接口。
b.实现registerBeanDefinitions参数AnnotationMetadata importClassMetadata是注解使用类(本例为JavaConfig)上@EnableB的元数据信息。
c.参数BeanDefinitionRegistry registry用来注册所有Bean的定义的接口。
d.可以使用BeanDefinitionBuilder来编程实现Bean的定义(BeanDefinition),此句定义了一个类型为String的Bean。
e.构造String的值是C。
f.设置Bean的Scope是singleton。
g.获得Bean的定义。
h.将Bean注册为名称为c的Bean。
此时,在JavaConfig上使用@EnableC注解,并用CommandLineRunner进行检验。

IntelliJ IDEA可以检测到静态注册的Bean,但检测不到动态注册的Bean,因而IDE会标识红色,如图3-11中方框所示。

图3-11
但可以正常运行,运行结果如图3-12所示。

图3-12
4.混合使用
@Import支持导入配置类的数组,因而我们可以混合上面三种配置,定义一个注解,使其具备上面三个功能。


因为选择器里指定了要使用的注解的类,所以需要新建一个选择器。

在JavaConfig中启用@EnableABC,并用CommandLineRunner进行检验。

校验结果如图3-13所示。

图3-13