在 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] 删除。
我来说两句