zoukankan      html  css  js  c++  java
  • spring读取配置文件,且获取bean实例

    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();
    }

    没有停止的脚步,只有倒下去的脚步
  • 相关阅读:
    组建小型局域网
    如何解决无法登陆微软账号
    常用网页收录入口
    Google正确搜索方法
    电脑无法识别U盘(usb类外接设备)的解决办法
    如何生成一副Poker
    Camtasia Studio8使用教程
    windows蓝屏代码大全及常见蓝屏解决方案
    [bzoj3712][PA2014]Fiolki
    [NOI2018]归程
  • 原文地址:https://www.cnblogs.com/hkMblogs/p/10795469.html
Copyright © 2011-2022 走看看