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

  • 相关阅读:
    linux中服务器定时程序设定
    Linux中java项目环境部署,简单记录一下
    四则运算使用栈和后缀表达式
    PAT乙1003
    L7,too late
    PAT乙1002
    L6,Percy Buttons
    如何计算递归算法的时间复杂度
    c#打印(转)
    C中数组与指针【转】
  • 原文地址:https://www.cnblogs.com/wujf/p/5261455.html
Copyright © 2011-2022 走看看