将POJO注释为Component

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将POJO注释为Component相关的知识,希望对你有一定的参考价值。

我可以通过@Component注释一个简单的pojo吗?

这是因为IntelliJ说:

无法自动发送,没有找到豆子

谢谢

答案

当然你可以使用@Component注释pojo,然后你可以在另一个用@Component注释的pojo中自动装入它,例如:

@Component
public class Computer {

    @Autowired
    Procesor procesor;

    public void printName() {
        System.out.println("486DX");
    }
}

@Component
public class Procesor {
}

@SpringBootApplication
public class SpringOneApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringOneApplication.class, args);
        Computer computer = (Computer) context.getBean("computer");
        computer.printName();
    }
}

以上是关于将POJO注释为Component的主要内容,如果未能解决你的问题,请参考以下文章