手动读取spring配置信息

    技术2022-05-20  39

    在根目录下配置applicationContext.xml文件或者系列文件

    1.小应用工厂

      import org.springframework.core.io.ClassPathResource;  import org.springframework.core.io.Resource;  Resource res = new ClassPathResource("applicationContext/_*.xml");        BeanFactory bf = new XmlBeanFactory(res);

            通过bf的getBean("beanName")方法得到bean  2.桌面应用

      import

    org.springframework.context.support.ClassPathXmlApplicationContext;  ApplicationContext context = new ClassPathXmlApplicationContext

    ("applicationContext/_*.xml");        通过context的getBean("beanName")方法   3.web应用开发

      import org.springframework.web.context.WebApplicationContext;  import

    org.springframework.web.context.support.WebApplicationContextUtils;  WebApplicationContext context =

    WebApplicationContextUtils.getWebApplicationContext(application);

           context.getBean("beanName");


    最新回复(0)