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

spring框架应用系列一:annotation-config自动装配

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

annotation-config自动装配

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

解决问题

通过spring XML配置文件,实现类对象之间松耦合

前提条件

使用的bean必须在spring容器中已注册过

内容说明

1、 使用@Autowired注解自动装配时,需在XML配置文件中引入 <context:annotation-config/>;

2、 存在多个bean满足装配属性,需用@Qualifier指定唯一的bean,否则会报异常;

Guitar和Saxophone都实现了Instrument接口,Instrumentalist存在一个属性类型为Instrument,

所以Instrumentalist满足装配Instrument属性的bean就有 Guitar和Saxophone,所以必须指定唯一的bean;

3、 bean 的id不指定时,默认类名小写;

例如<bean class="com.spring.example.annotation.config.Guitar"/> 因为id没有指明,所以默认为guitar;

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

spring配置文件 annotation-config.xml 如下:

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        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">
 6 
 7     <!--&lt;!&ndash;使用基于注解自动装配 @Autowired @Inject @Resource&ndash;&gt;-->
 8     <context:annotation-config/>
 9 
10 
11 
12     <!--限定歧义性的依赖,使用@Autowired注解自动装配,满足装配的多个bean,
13     可以通过@Qualifier指定来缩小范围 默认byType
14     Guitar和Saxophone都实现了Instrument,所以有多个bean满足Instrumentalist注入属性,
15     需要指定唯一bean,用@Qualifier("guitar")指定,否则会报异常
16     -->
17 
18     <bean id ="guitar" class="com.spring.example.annotation.config.Guitar"/>
19     <bean id ="saxophone" class="com.spring.example.annotation.config.Saxophone"/>
20     <bean id ="kenny"
21     class="com.spring.example.annotation.config.Instrumentalist">
22     <property name="song" value="Jingle Bells3" />
23     </bean>
24 
25 </beans>

Instrument接口代码

代码语言:javascript
复制
public interface Instrument {
    void play();
}

Guitar实现接口Instrument代码

代码语言:javascript
复制
public class Guitar implements Instrument {
    @Override
    public void play() {
        System.out.println("Guitar....");
    }
}

Saxophone实现接口Instrument代码

代码语言:javascript
复制
public class Saxophone implements Instrument {
    @Override
    public void play() {
        System.out.println("Saxophone ......");
    }
}

Performer接口代码

代码语言:javascript
复制
public interface Performer {

    void perform();
}

Instrumentalist实现接口Performer代码

代码语言:javascript
复制
public class Instrumentalist implements Performer {

    public Instrumentalist(){}
    @Value("Yesterday Once more !") //song 初始化值
    private String song;

//    @Autowired 可以装配属性、方法、构造函数,只要类型相同(这里是Instrument类型)
    @Autowired
    @Qualifier("guitar") //spring容器中有多个bean满足要求,需要指定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();
    }

}

测试代码

代码语言:javascript
复制
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/annotation-config.xml");
            Performer performer = (Performer) ctx.getBean("kenny");
            performer.perform();

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

运行结果

总结

annotation-config下@Autowired自动装配自然也有其优缺点:

优点:实现类对象之间松耦合,

缺点:需要在spring配置文件中需提前手动指明bean,增加XML文件繁琐冗余性;

在业务逻辑开发中不能将业务类自动注入spring容器,遇到业务类的更改,配置文件也不得不更改,增加出错概率;

component-scan能解决此问题,会在下节讲述;

应用场景

调用第三方接口,装配注入相应实例bean;

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

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

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

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

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

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

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