博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring读取配置文件,且获取bean实例
阅读量:4532 次
发布时间:2019-06-08

本文共 1289 字,大约阅读时间需要 4 分钟。

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://www.cnblogs.com/hkMblogs/p/10795469.html

你可能感兴趣的文章
洛谷P1072 Hankson 的趣味题
查看>>
R语言演示功能
查看>>
python IO 多路复用 epoll简单模型
查看>>
LeetCode: 103_Binary Tree Zigzag Level Order Traversal | 二叉树Zigzag层次遍历 | Medium
查看>>
【记忆化搜索】[NOIP-2017--普及组] -- 棋盘
查看>>
MyBatis_[tp_48]_动态sql_内置参数_parameter&_databaseId
查看>>
Storm 集群安装
查看>>
使用情感分析技术做营销
查看>>
基于Token的WEB后台认证机制
查看>>
高级树状数组——区间修改区间查询、二维树状数组
查看>>
创造者模式
查看>>
element-ui 上传图片 后清空 图片 显示
查看>>
试玩汇编语言 1:环境搭建
查看>>
golang的的模板引擎之pongo2
查看>>
git中Please enter a commit message to explain why this merge is necessary
查看>>
Backbone.js 资源搜集
查看>>
CSS优先级问题以及jQuery中的.eq()遍历方法和:eq()选择器的差别
查看>>
Remote desktop connect to Opensuse 11 using xmanager on Windows 7
查看>>
使用Navicat连接oracle时出现unsupported server character set ZHS16GBK的解决之道
查看>>
tensorflow Sigmoid 应用
查看>>