首页 > 其他分享 >第5章-Spring三种配置方式的混合和迁移

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

时间:2022-11-27 16:12:36浏览次数:63  
标签:XML Spring 配置 Bean 三种 注解 迁移 属性

目录

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

标签:XML,Spring,配置,Bean,三种,注解,迁移,属性
From: https://www.cnblogs.com/code-artist/p/spring-5.html

相关文章

  • 第6章-Spring同类型多个Bean的注入
    目录一、类型注入冲突二、解决冲突1.注入主要的2.注入指定的三、注入多个Bean1.注入集合2.注入Map3.Bean的顺序四、附录1.常用注解2.示例代码Spring容器中的B......
  • 第3章-Spring基于注解配置的容器
    目录一、Bean管理1.扫描类路径配置2.使用注解管理Bean二、依赖管理1.依赖注入1.1字段注入1.2构造器注入1.3Setter方法注入2.依赖关系3.懒加载三、附录1.配置属......
  • 第2章-Spring基于XML配置的容器
    目录一、Bean管理1.元数据2.Bean实例化2.1构造器实例化2.2静态工厂方式实例化2.3实例工厂方式实例化3.Bean作用域二、依赖注入1.依赖注入1.1构造器注入1.2Sett......
  • Spring mvc整合hibernate例子
    企业级项目实战(带源码)地址:[url]http://zz563143188.iteye.com/blog/1825168[/url]收集五年的开发资料及源码下载地址:[url]http://pan.baidu.com/......
  • spring gateway路由出现503、404错误解决方法
    查资料发现在网关出现503错误是因为全局过滤器没有加载(ReactiveLoadBalancerClientFilter),只需要将含有这个过滤器的依赖进行导入就行了<dependency><groupId>org.......
  • Spring中@Autowired注解、@Resource注解的区别
    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。@Resource的作用相当于@Autowired,只......
  • Spring之可扩展点
    一、SpringBean的生命周期    二、后置处理器postProcessor 一个是针对BeanDefinition的容器级别的后处理器-BeanFactoryPostProcessor一个是针对getBean......
  • 【Java】Springboot 实现数据脱敏
     实现效果:1、脱敏注解在模型类进行标记packagecn.cloud9.server.test.model;importcn.cloud9.server.struct.masking.annotation.MaskingField;importcn.cloud9......
  • spring::注解开发
    @Required文档@Required注释应用于bean属性的setter方法,它表明受影响的bean属性在配置时必须放在XML配置文件中,否则容器就会抛出一个BeanInitializationExcept......
  • 【Spring Cloud实战】Hystrix断路器
    gitee地址:https://gitee.com/javaxiaobear/spring-cloud_study.git在线阅读地址:https://javaxiaobear.gitee.io/1、概述分布式面临的问题复杂分布式体系结构中的应用程序有......