在 spring 框架中,如果我使用注释而不是 XML 配置,我是否需要在单独的类中初始化 spring 容器?

阿米特·K

在 spring 框架中,如果我使用注释而不是 XML 配置,我是否需要在一个单独的类中初始化 spring 容器,如下所示。

类 AppContextProj{

    public static void main(String [] args){
            ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    }

}

罗曼·普契科夫斯基
ApplicationContext context = new AnnotationConfigApplicationContext(YourConfiguration1.class, YourConfiguration2.class, ...);

@Configuration在构造函数中接受-annotated 类。

或者

ApplicationContext context = new AnnotationConfigApplicationContext("com.company.basepackage1", "com.company.basepackage2", ...);

它将自动扫描指定包中的@Configuration- 和 -@Component注释类(包括@Service@Repository等等)并选择它们。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章