创建对象并赋值
@Test
public void fromSrc() {
String path = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(path);
TestService ts = (TestService) ac.getBean("testService");
ts.interfaceOne();
}
@Test
public void fromRoute() {
String path = "e:" + File.separator + "path" + File.separator + "eclipsepathOfdljdfuxi" + File.separator
+ "applicationContext.xml";
ApplicationContext ac = new FileSystemXmlApplicationContext(path);
TestService ts = (TestService) ac.getBean("testService");
ts.interfaceOne();
}
/**
* 配置文件放置根目录下
*/
@Test
public void fromRootDirectory() {
String path = "applicationContext.xml";
ApplicationContext ac = new FileSystemXmlApplicationContext(path);
TestService ts = (TestService) ac.getBean("testService");
ts.interfaceOne();
}
/**
* 对象是在什么时候创建的
*/
@Test
public void fromMakeObject() {
String path = "applicationContext.xml";
ApplicationContext ac = new FileSystemXmlApplicationContext(path);
TestService ts = (TestService) ac.getBean("testService");
ts.interfaceOne();
}
/**
* 获取容器创建的个数
*/
@Test
public void fromObjectNum() {
String path = "applicationContext.xml";
ApplicationContext ac = new FileSystemXmlApplicationContext(path);
int count = ac.getBeanDefinitionCount();
System.out.println("容器创建的个数:" + count);
String[] names = ac.getBeanDefinitionNames();
for (String string : names) {
System.out.println("容器对象是:" + string);
}
}
本文探讨了Spring框架中如何从不同路径加载配置文件,并通过Spring容器创建对象的过程。包括从类路径、文件系统和根目录加载配置,以及获取容器中创建的对象数量。
633

被折叠的 条评论
为什么被折叠?



