常见的有FileSystemXmlApplicationContext、ClassPathXmlApplicationContext。

举个例子就是:

String[] locations = new String[]{"classpath*:person.xml"};
// 读取配置文件
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
// 从Spring容器中取值
Person p = ctx.getBean("person", Person.class);
LOG.debug(p.toString());
ctx.close();

如上代码中把ClassPathXmlApplicationContext换成GenericXmlApplicationContext也能正常运行。

看了Spring部分源码后发现其实用AnnotationConfigApplicationContext也能启动。如其名,该类用于以注解方式启动Spring的。代码如下:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 要扫描的包名,不含有变量
String basePackage = "jee.billy.springstudy.bean";
// 要扫描的包名,包名中含有变量(在windows系统中sun.desktop的值为'windows')
String basePackageWithVariable = "jee.billy.springstudy.${sun.desktop}.newbean";
// 扫描
context.scan(basePackage, basePackageWithVariable);
context.refresh();
// 获取Bean并打印
Person p = context.getBean(PersonAnnotation.class);
LOG.debug(p.toString());
WindowsBean windowsBean = context.getBean(WindowsBean.class);
LOG.debug(windowsBean.toString());
context.close();

继续了解中。