Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

@Component重新给bean命名后,为什么@Resource找不到?

CompactDisc有多个实现类,我想要注入其中的一个

@Component
public class SgtPeppers implements CompactDisc {
    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";
    public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }

}

图片描述

用默认的bean可以

但是我把bean id改成其他,比如

@Component("segment")
public class SgtPeppers implements CompactDisc {
    ...
}

图片描述

怎么就找不到了?

3月10号更新,今天又可以了,真是奇怪,还是没找到出现问题的关键

3月17号:感觉是IDE的问题


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这肯定找不到啊,你用@Component就是自动注入,然后注入的名字就是sgtPeppers.
@Resource是根据name取,你名字不对怎么能取对呢.
补充一点自动注入名称是根据类名 然后第一个字母小写来注入的名字


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...