zoukankan      html  css  js  c++  java
  • 学习之spring注解DI疑惑

    接口定义

    package com;
    
    public interface IPaly {
        void say();
    }

    接口实现类

    package com;
    
    import org.springframework.stereotype.Service;
    
    //@Service(value = "Play") //默认的value为类名首字母小写
    @Service
    public class Play implements IPaly {
    
        public void say()
        {
            System.out.println("我是注解!!!"+this.getClass());
        }    
    }
    package com;
    
    import org.springframework.stereotype.Service;
    
    //@Service(value = "Play2")
    @Service
    public class Play2 implements IPaly {
    
        public void say() {
            System.out.println("我是注解!!!" + this.getClass());
        }
    }

    service接口注入

    package com;
    
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    
    //配置非单例
    @Scope("prototype")
    @Service
    public class Person {
        @Autowired
        @Qualifier("play")
    //    @Resource(name="play2")
        private IPaly user;
    
        public void say() {
            user.say();
        }
    }

    要么使用限制名,要么使用Resource注入并指定名字,否则会报错

    错误描述的意思就是有两个接口实现

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    用js遍历生成数独可行数据(未优化版本)
    JS生成tips小工具
    Iframe使用
    二级指针作输入的三种内存模型
    货品的进出库模型
    约瑟夫问题
    vector
    CUDA并行简单加法
    第一个CUDA程序
    在Ubuntu下安装、配置和测试cuda[复制]
  • 原文地址:https://www.cnblogs.com/wujf/p/5261455.html
Copyright © 2011-2022 走看看