美文网首页Spring高效实践
第5章-Spring三种配置方式的混合和迁移

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

作者: 码匠_CodeArtist | 来源:发表于2021-12-04 10:56 被阅读0次

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 码匠公众号。

相关文章

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

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

  • 6、Spring-XML配置

    一、概要 配置方式 Spring支持三种方式配置Bean,Spring1.0仅支持基于XML的配置,Spring2...

  • SpringBoot源码分析-004 创建容器

    第三步: 总结: Spring bean配置方式有三种:基于XML的配置方式 、基于注解的配置方式和基于Java类...

  • SpringAOP使用

    目前 Spring AOP 一共有三种配置方式。 Spring 1.2 基于接口的配置:最早的 Spring AO...

  • Spring配置方式

    Spring有三种配置方式: XML配置 注解配置(annotation) Java配置 XML配置 最经典 注解...

  • RabbitMQ如何保证消息的可靠投递?

    Spring Boot整合RabbitMQ Spring有三种配置方式 基于XML 基于JavaConfig 基于...

  • Spring Bean 配置

    Spring Bean 配置三种方式: XML显式配置 Java中显式配置 隐式Bean发现机制和自动装配。 隐式...

  • 精进 Spring Boot 03:Spring Boot 的配

    内容简介:本文介绍 Spring Boot 的配置文件和配置管理,以及介绍了三种读取配置文件的方式,并进行代码演示...

  • Spring 注解使用

    常用注解 Spring中有三种配置方式:基于XML的配置、基于注解的配置、基于Java的配置。 使用原则: 1、S...

  • Spring学习-1

    一:Spring中配置文件的加载原理: 二:常用的Spring配置文件的加载方式: 有三种分别是: 1:使用我们当...

网友评论

    本文标题:第5章-Spring三种配置方式的混合和迁移

    本文链接:https://www.haomeiwen.com/subject/eauvxrtx.html