3.0新增容器启动方法

在3.0之前的Spring核心框架中,我们启动一个Spring容器必须使用一个XML文件。而到了3.X之后的版本Spring为创建容器新增了一个入口类——AnnotationConfigApplicationContext。

AnnotationConfigApplicationContext和过去的ClassPathXmlApplicationContext、FileSystemXmlApplicationContext等方法不同的是他不用再指定任何XML配置文件,而是可以通过指定类向容器添加Bean。我们通过几个简单的例子来说明他的使用。

(以下例子只用于说明问题,源码请到 gitee 自行 clone,本节的代码在 chkui.springcore.example.javabase.simple 包中)。

直接添加Bean

我们可以通过AnnotationConfigApplicationContext直接向容器添加指定的类作为Bean,先定义我们的class:

package chkui.springcore.example.javabase.simple.pureBean;class LolBean { public String toString() { return "I AM LOL!"; }}class WowBean { public String toString() { return "I AM WOW!"; }}

然后向容器添加这些Bean:

package chkui.springcore.example.javabase.simple;public class WithoutAnnotation { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(WowBean.class, LolBean.class); System.out.println(ctx.getBean(WowBean.class)); System.out.println(ctx.getBean(LolBean.class)); }}

这样就启动了一个Spring的容器,并且容器中包含了WowBean和LolBean这两个类的单例。

替代标签

@Configuration在之前介绍Spring核心容器的文章中出现过一两次,配合各种注解的使用@Configuration可以替代配置中的所有功能。基本上AnnotationConfigApplicationContext和@Configuration组合使用就可以实现Spring容器纯Java启动。请看下面的例子。

我们在前面例子的基础上增加几个类:

package chkui.springcore.example.javabase.simple.bean;public class DotaBean { public String toString() { return "I AM Dota!"; }}@Componentpublic class PseBean { @Override public String toString() { return "I AM PSE!"; }}

注意DotaBean上是没有@Component注解的。然后添加@Configuration配置:

package chkui.springcore.example.javabase.simple.bean;@Configuration@ComponentScan("chkui.springcore.example.javabase.simple.bean")public class Config { @Bean public DotaBean dotaBean() { return new DotaBean(); }}

最后运行他们:

package chkui.springcore.example.javabase.simple;public class WithScan { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, WowBean.class, LolBean.class); System.out.println(ctx.getBean(Config.class)); System.out.println(ctx.getBean(PseBean.class)); System.out.println(ctx.getBean(WowBean.class)); System.out.println(ctx.getBean(LolBean.class)); System.out.println(ctx.getBean(DotaBean.class)); }}

@Component已经在 Stereotype组件与Bean扫描 这篇文章介绍过,@ComponentScan的作用等价于标签,属性参数都是一一对应的,只不过前者是驼峰命名规则(camelCase)——@ComponentScan(basePackages="..."),后者是短横线命名规则(kebab-case)——。实际上使用Annotation来替换XML配置中的内容,大部分都使用这种转换方式。

@Configuration和@Bean标签会在后续的内容中详细介绍。@Bean主要用于方法标记,表明这个方法返回一个要添加到容器中的Bean。

AnnotationConfigApplicationContext的其他使用方法

除了以上常规的使用方法,AnnotationConfigApplicationContext还有其他方式向容器添加Bean。

可以使用AnnotationConfigApplicationContext::register方法来添加配置和Bean:

public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); //动态添加配置文件 ctx.register(Config1.class, Config2.class); //动态添加Bean ctx.register(Bean1.class); //刷新 ctx.refresh();}

注意最后的refresh方法,这个方法来源于ConfigurableApplicationContext接口,然后是在AbstractApplicationContext中实现的。他的过程相当于销毁之前已经创建的资源,然后再重新创建了一个新的容器。这里的代码会执行以下几步:

new AnnotationConfigApplicationContext():创建一个新的容器,容器中没有自定义的Bean。AnnotationConfigApplicationContext::register:向容器添加BeanDefinition,但是这些BeanDefinition并没有转化为容器中的Bean。ConfigurableApplicationContext::refresh():纳入新添加的BeanDefinition重建容器。

还可以直接使用AnnotationConfigApplicationContext::scan方法扫描指定的路径:

public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan("com.acme"); ctx.refresh();}

执行原理和上面介绍的一样。

需要注意的是:如果你的工程中需要使用AnnotationConfigApplicationContext::register、AnnotationConfigApplicationContext::scan等方法创建容器和其中Bean的依赖关系,所有的Bean都只能在register或scan中添加。如果你既在AnnotationConfigApplicationContext的构造方法中添加了Bean,又使用AnnotationConfigApplicationContext::refresh()方法会抛出一个重复执行refresh的异常。AnnotationConfigApplicationContext::refresh()方法全局也只能被调用一次。

@Bean注解

@Bean注解等价于配置文件中的标签,对应的参数也是将短横线命名切换为驼峰命名—— => @Bean(initMethod="...")。@Bean注解只能使用在方法上,方法必须是在@Configuration标记的类或者其他Bean中,两者存在的差异会在后续的文章中介绍。下面通过一个例子来说明Bean的使用。

(以下例子只用于说明问题,源码请到 gitee 自行 clone,本节的代码在 chkui.springcore.example.javabase.beanAnnotation 包中)。

定义两个要添加到容器中的Bean:

package chkui.springcore.example.javabase.beanAnnotation.bean;class FinalFantasy { @Override public String toString() { return "Final Fantasy 1~15"; } public void init() { System.out.println("Final Fantasy init!"); } public void destroy() { System.out.println("Final Fantasy destroy!"); }}class DragonQuest { public String toString() { return "Dragon Quest 1~11"; } @PostConstruct public void init() { System.out.println("Dragon Quest init!"); } @PreDestroy public void destroy() { System.out.println("Dragon Quest destroy!"); }}

定义一个功能接口及其实现类:

package chkui.springcore.example.javabase.beanAnnotation.bean;interface Support { void setFinalFantasy(FinalFantasy ff); FinalFantasy getFinalFantasy();}class SupportImpl implements Support { private FinalFantasy ff; public void setFinalFantasy(FinalFantasy ff) { this.ff = ff; } public FinalFantasy getFinalFantasy() { return ff; }}

然后顶一个@Configuration类:

package chkui.springcore.example.javabase.beanAnnotation.bean;public class BeanAnnotationConfig { @Bean public Support support(FinalFantasy ff) { Support support = new SupportImpl(); support.setFinalFantasy(ff); return support; } @Bean(initMethod="init", destroyMethod="destroy") @Description("Final Fantasy") public FinalFantasy finalFantasy() { return new FinalFantasy(); } @Bean(name= {"dragon-quest", "DragonQuest"}) public DragonQuest dragonQuest() { return new DragonQuest(); }}

最后运行他们:

public class BeanAnnotApp { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanAnnotationConfig.class); Support support = ctx.getBean(Support.class); System.out.println(support.getFinalFantasy()); System.out.println(ctx.getBean(DragonQuest.class)); }}

在配置类BeanAnnotationConfig中,我们配置了3个Bean。这里的写在方法上的@Bean注解和写在配置文件中的注解一个效果:

@Bean中的initMethod和destroyMethod对应标签中的init-method和destroy-method属性。@Bean中的name参数只有一个值时相当于id,有多个的时候相当于设置了多个别名Support support(FinalFantasy ff):我们可以直接在方法中暴露参数来引入其他Bean,这就类似于配置中ref的功能。如果不指定initMethod和destroyMethod,使用JSR-330的生命周期注解(@PostConstruct、@PreDestroy)同样有效

查看原文 >>
相关文章