如何在 Spring Framework 4 中初始化应用程序上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22885339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize Application Context in Spring Framework 4
提问by SergeZ
I have a project based on Spring Framework 4and its subproject - Spring Data Solr.
我有一个基于Spring Framework 4及其子项目 - Spring Data Solr 的项目。
All examples I had a chance to see explain how to organize your project - from base entity ( pojo's ) classes till spring specific classes such as repositories and service. When it came to test functionality all examples show a test with private field ( spring bean ) which usually becomes initialized with the help of annotation
我有机会看到的所有示例都解释了如何组织您的项目 - 从基本实体(pojo 的)类到 Spring 特定类,例如存储库和服务。当谈到测试功能时,所有示例都显示了一个带有私有字段(spring bean)的测试,它通常在注释的帮助下初始化
@ContextConfiguration(classes = some-spring-data-main-class.class, loader = SpringApplicationContextLoader.class)
Then it is possible to call for it's bean's methods in @Test
methods.
然后可以在@Test
方法中调用它的 bean方法。
But, when it comes to init bean in a project - how to make it with Spring 4, which is completely XMLless ( I mean, I do not have an applicationContext xml
file ).
但是,当涉及到项目中的 init bean 时 - 如何使用 Spring 4 来实现它,它完全没有 XML(我的意思是,我没有applicationContext xml
文件)。
P.S. in Spring 3 we usually wrote smth like:
PS 在 Spring 3 中我们通常这样写:
ApplicationContext context = new ClasspathApplicationContext("applicationContext.xml")
Is it reasonable to expect smth similar to this of Spring 4 introduces absolutely new concepts of application initialization? What should we write now to init app's first bean?
期待与 Spring 4 类似的 smth 引入绝对新的应用程序初始化概念是否合理?我们现在应该写什么来初始化应用程序的第一个 bean?
采纳答案by SergeZ
I got it !
我知道了 !
In Spring 4 now we have to write:
在 Spring 4 现在我们必须写:
ApplicationContext context = new AnnotationConfigApplicationContext(<out-main-config-class>.class);
and then call for beans and its methods.
然后调用 bean 及其方法。