【问题标题】:Annotate a POJO as Component将 POJO 注释为组件
【发布时间】:2018-08-11 03:50:08
【问题描述】:

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

这是因为 IntelliJ 说:

无法自动装配,没有找到 bean

谢谢

【问题讨论】:

  • IntelliJ 说在哪里可以使用注解!
  • 为什么要设置pojo为组件?
  • pojo中的注释应该是什么?因为intellij 表示Incant 为pojo 做@autowire ...

标签: java spring components


【解决方案1】:

当然你可以用@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();
    }
}

【讨论】:

    猜你喜欢
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2013-05-27
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    相关资源
    最近更新 更多