前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring框架应用系列二:component-scan自动扫描注册装配

spring框架应用系列二:component-scan自动扫描注册装配

作者头像
用户7225427
发布2020-09-03 10:17:27
4650
发布2020-09-03 10:17:27
举报
文章被收录于专栏:厚积薄发厚积薄发

component-scan自动扫描注册装配

本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7717331.html

解决问题

通过component-scan自动扫描将业务逻辑bean注册到spring容器中,去除XML配置文件bean手动注册过程,降低XML配置文件繁琐性;

内容说明

1、注册扫描bean并使用@Autowired注解自动装配时,需在XML配置文件中引入 <context:component-scan base-package="com.spring.example.scan"/>;

2、通过component-scan自动扫描定义基类包下所有bean,需要在类名前加入注解@Component,并且可以自定义bean的id<@Component("instru")>,

如果没有定义bean的id就默认类名(全部小写),将扫描的bean装入spring容器;

3、 在spring容器启动后(获取到spring容器的实例),通过Autowired注解以及bean的id(Qualifier)来装配和注入相应的实例对象;

4、 调用实例方法;

应用实例(包名:com.spring.example.scan)

spring配置文件component-scan.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--自动扫描检测bean以及定义bean,并将bean放入spring容器-->
    <context:component-scan base-package="com.spring.example.scan"/>

    
</beans>

Instrument接口代码

public interface Instrument {
    void play();
}

Guitar实现接口Instrument代码

@Component
public class Guitar implements Instrument {
    @Override
    public void play() {
        System.out.println("Guitar....");
    }
}

Performer接口代码

public interface Performer {

    void perform();
}

Instrumentalist实现接口Performer代码

@Component("instru") //指定bean id
public class Instrumentalist implements Performer {

    public Instrumentalist(){}

    @Value("Yesterday Once more !")
    private String song;


    //    @Autowired 可以装配属性、方法、构造函数,只要类型相同(这里是Instrument类型)
   //    限定歧义性的依赖,使用@Autowired注解自动装配,满足装配的多个bean,
   //    可以通过@Qualifier指定来缩小范围
    @Autowired
    @Qualifier("guitar") //通过id指定bean
    private Instrument instrument;

    public void setSong(String song) {
        this.song = song;
    }

    public void setInstrument(Instrument instrument){
        this.instrument = instrument ;
    }

    @Override
    public void perform() {
        System.out.println("Playing "+ song + " : ");
        instrument.play();
    }

}

测试代码

public class Driver extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        try {

            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/component-scan.xml");

            Performer performer = (Performer) ctx.getBean("instru");
            performer.perform();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

运行结果

总结

component-scan自动扫描优缺点:

优点: a> component-scan自动扫描注册装配可以有效解决annotation-config的不足,不需要在spring配置文件中需提前指明bean,使配置文件非常简洁;

b> 去除在XML定义bean的过程,在应用程序中直接装配和注入,减少更改类名实导致的错误;

缺点: 不能自动获取第三方接口实例bean;

应用场景

适用于自身业务逻辑开发;

本文描述可能有不对或不全之处,欢迎大家吐槽!

不要让懒惰占据你的大脑,不要让妥协拖垮你的人生。青春就是一张票,能不能赶上时代的快车,你的步伐掌握在你的脚下。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • component-scan自动扫描注册装配
    • 解决问题
      • 内容说明
        • 应用实例(包名:com.spring.example.scan)
          • 应用场景
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档