import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; / * 实例化容器测试类 * */ public class Test { public static void main(String[] args){ //方式一:在CLASSPATH路径下获取XMLBeanFactory实例 ClassPathResource res = new ClassPathResource("container.xml"); XmlBeanFactory factory = new XmlBeanFactory(res); HelloBean hellobean = (HelloBean)factory.getBean("helloBean"); hellobean.sayHelloWorld(); //方式二:指定绝对路径建ApplicatinContext实例 FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:\\My_Struts_Cvs8\\springioc\\src\\container.xml"); BeanFactory factory2 = (BeanFactory) context; HelloBean hellobean2 = (HelloBean)factory2.getBean("helloBean"); hellobean2.sayHelloWorld(); //方式三:通过ClassPathXmlApplicationContext创建BeanFactory实例 ClassPathXmlApplicationContext context3 = new ClassPathXmlApplicationContext("container.xml"); BeanFactory factory3 = (BeanFactory) context3; HelloBean hellobean3 = (HelloBean)factory3.getBean("helloBean"); hellobean3.sayHelloWorld(); } }
讯享网

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/77213.html