Home  >  Q&A  >  body text

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

PHP中文网PHP中文网2741 days ago1023

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:51:10

    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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:51:10

    You need to specify @Component("segment")

    reply
    0
  • Cancelreply