Spring中获取ApplicationContext对象的方法有哪些?

Gna*_*i A 0 java spring

嗨,我想知道在 Spring 中获取 ApplicationContext 对象的不同方法是什么?我只知道一种方式,那就是

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Run Code Online (Sandbox Code Playgroud)

还有其他方法吗?如果是,请告诉我。

谢谢。

Evg*_*eev 5

您还可以使用基于注释的配置

@Configuration
public class Config {

    @Bean
    public Bean1 bean1() {
        return new Bean1();
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    }
}
Run Code Online (Sandbox Code Playgroud)