【发布时间】:2021-03-18 07:51:35
【问题描述】:
public class UserInterface{
public static void main(String args[]){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
AbcClass abc = applicationContext.getBean(AbcClassImpl.class);
AbcClass cde = new AbcClassImpl();
}
}
只是想知道如果我们已经为此创建了 Bean,是否可以使用 new 关键字创建。
【问题讨论】:
-
你可以。但如果您通过
new关键字创建对象,则自动装配在其中不起作用。如果您需要多个 bean 实例,则应查看原型范围 -
@Jens 谢谢,知道了。是的,这就是为什么我在尝试使用“cde”对象访问 Autowired 类的方法时得到 NullPointerException。
标签: java spring spring-boot dependency-injection