如何在 Spring Framework 4 中初始化应用程序上下文

Ser*_*geZ 5 spring launch javabeans applicationcontext

我有一个基于Spring Framework 4及其子项目 - Spring Data Solr 的项目。

我有机会看到的所有示例都解释了如何组织您的项目 - 从基本实体( pojo )类到 Spring 特定类(例如存储库和服务)。当谈到测试功能时,所有示例都显示带有私有字段( spring bean )的测试,该字段通常在注释的帮助下进行初始化

@ContextConfiguration(classes = some-spring-data-main-class.class, loader = SpringApplicationContextLoader.class)
Run Code Online (Sandbox Code Playgroud)

然后就可以在方法中调用它的bean的方法了@Test

但是,当涉及到项目中的 init bean 时 - 如何使用 Spring 4 来实现它,它是完全无 XML 的(我的意思是,我没有文件applicationContext xml)。

PS 在 Spring 3 中我们通常这样写:

ApplicationContext context = new ClasspathApplicationContext("applicationContext.xml") 
Run Code Online (Sandbox Code Playgroud)

期望与 Spring 4 类似的东西引入应用程序初始化的全新概念是否合理?现在我们应该写什么来初始化应用程序的第一个 bean?

Ser*_*geZ 4

我得到了它 !

现在在 Spring 4 中我们必须这样写:

ApplicationContext context = new AnnotationConfigApplicationContext(<out-main-config-class>.class);
Run Code Online (Sandbox Code Playgroud)

然后调用bean及其方法。