第5章-Spring三种配置方式的混合和迁移


Spring 容器的配置方式有三种,在实际项目中,可能会遇到混合使用的情况,也可能需要从一种方式迁移到另一种方式。

一、配置兼容

Spring 的三种配置方式混合使用,需要兼容 XML 配置和注解配置。

1. 优先 XML 配置

在老的 Spring 项目中,常常会存在大量的 XML 配置,但并不影响我们使用注解配置的新特性。

XML 配置中使用注解配置

配置文件中使用 context:component-scan 来配置扫描的包路径。

该路径下的类就支持 @Configuration@Component@Autowired 等注解。

<beans>

    <context:component-scan base-package="cn.codeartist.spring.bean.mix"/>

</beans>

XML 配置中使用 Java 配置

扫描的包路径下使用 @Configuration 定义配置类,在配置类中使用 @Bean 注册 Bean。

2. 优先注解配置

老的 Spring 项目在迁移的过程中,可能需要在使用注解和 Java 的配置中,使用 XML 配置。

注解配置中使用 XML 配置

在配置类上使用 @ImportResource 来导入 XML 配置文件。

@Configuration
@ImportResource("classpath:bean.xml")
public class AppConfig {

}

classpath:bean.xml 表示基于 classpath 路径的资源文件。

注解配置中使用 Java 配置

直接在配置类中使用 @Bean 注册 Bean。

@Configuration
@ComponentScan("cn.codeartist.spring.bean.mix")
public class AppConfig {

    @Bean
    public BeanExample beanExample() {
        return new BeanExample();
    }
}

或者在扫描的包路径下使用 @Configuration 注解定义配置类。

二、迁移方案

基于 XML 配置的容器使用 ClassPathXmlApplicationContextFileSystemXmlApplicationContext 实例化。

基于注解配置的容器使用 AnnotationConfigApplicationContext 实例化。

// XML
public static void main(String[] args) {
    ApplicationContext applicationContext =
        new ClassPathXmlApplicationContext("bean.xml");
    BeanExample beanExample = (BeanExample) applicationContext.getBean("beanExample");
}

// 注解
public static void main(String[] args) {
    ApplicationContext applicationContext =
        new AnnotationConfigApplicationContext(AppConfig.class);
    BeanExample beanExample = (BeanExample) applicationContext.getBean("beanExample");
}

1. XML 配置至注解配置

配置文件中添加 context:component-scan 指定扫描的包路径。

2. XML 配置至 Java 配置

XML 配置中的<beans><bean>标签,等效于 Java 配置中的@Configuration@Bean配置。

<beans>

    <bean id="beanExample" class="cn.codeartist.spring.bean.mix.BeanExample"/>

</beans>

等效于:

@Configuration
public class AppConfig {

    @Bean
    public BeanExample beanExample() {
        return new BeanExample();
    }
}

属性对照

XML 配置和注解配置对应属性迁移。

XML配置注解配置
<context:component-scan>@ComponentScan
<bean>id 属性@Beanvaluename 属性
<bean>scope 属性@Scope
<bean>depends-on 属性@DependsOn
<bean>lazy-init 属性@Lazy
<bean>primary 属性@Primary
<bean>init-method 属性@BeaninitMethod 属性
<bean>destroy-method 属性@BeandestroyMethod 属性

三、附录

1. 配置属性

属性描述
context:component-scan在基于 XML 配置容器中,指定扫描包路径

2. 常用注解

注解描述
@Configuration指定 Bean 的配置类
@ComponentScan(默认为类所在的包)指定包路径,该包下的类由容器管理
@Component指定该类由 Spring 容器管理
@ImportResource注解配置中导入 XML 配置文件

3. 示例代码

Gitee 仓库:https://gitee.com/code_artist/spring
项目模块:spring-ioc
示例路径:cn.codeartist.spring.bean.mix

最新文章关注 CodeArtist 码匠公众号!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码匠_CodeArtist

指出错误和提出建议也是一种打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值