java - @Component重新给bean命名后,为什么@Resource找不到?
PHP中文网
PHP中文网 2017-04-18 10:49:10
0
2
1070

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的问题

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
伊谢尔伦

You definitely can’t find it. You use @Component to automatically inject, and then the name of the injection is sgtPeppers.
@Resource is based on the name. How can you get it right if your name is wrong?
In addition, the name of the automatic injection is The name injected is based on the class name and the first letter is lowercase

Peter_Zhu

You need to specify @Component("segment")

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template