学习之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

原文地址:https://www.cnblogs.com/wujf/p/5261455.html