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

  • 相关阅读:
    Python学习————并发编程
    Python学习————作业
    Python学习————网络编程
    Python学习————异常处理
    Python学习————反射
    Python学习————绑定方法
    Python学习————继承
    1765 谷歌的恐龙
    2504 是子序列的个数
    51Nod2386 分则能成
  • 原文地址:https://www.cnblogs.com/wujf/p/5261455.html
Copyright © 2011-2022 走看看