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

spring读取配置文件,且获取bean实例的更多相关文章

  1. Spring读取配置文件,获取bean的几种方式

    BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...

  2. SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

    分类: [java]2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路 ...

  3. Spring 读取配置文件(二)

    Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...

  4. Spring 读取配置文件(一)

    注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...

  5. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  6. Spring源码浅析之bean实例的创建过程(一)

    在之前的文章内容中,简单介绍了bean定义的加载过程,下面这篇的主要内容就是bean实例的创建过程. bean实例的创建方式 ApplicationContext context = new Clas ...

  7. Spring通过ApplicationContext主动获取bean

    有些场景无法通过AutoWired和compoment注解传递进来,于是希望通过Spring context主动去获取beandemo: package com.qhong.Util; import ...

  8. Spring容器中获取bean实例的方法

    // 得到上下文环境 WebApplicationContext webContext = ContextLoader .getCurrentWebApplicationContext(); // 使 ...

  9. spring-如何在项目启动的情况下获取Bean实例

    十年阿里,就只剩下这套Java开发体系了 >>>   大家都知道,项目启动的时候,spring读取xml文件,将配置的bean 或者 注解下的controller service d ...

随机推荐

  1. 如何修改AWR的retention,interval

    检查AWR当前设置: SQL> select * from dba_hist_wr_control; DBID SNAP_INTERVAL RETENTION TOPNSQL --------- ...

  2. 如何创建和配置Solaris10 zones (ZT)

    http://thegeekdiary.com/how-to-create-and-configure-solaris-10-zones/ Solaris zones enables a softwa ...

  3. js和jQuery判断数组是否包含指定元素

    最近遇见一些前台基础性问题,在这里笔者觉得有必要记录一下,为了以后自己查阅或者读者查看. 已知var arr = ['java','js','php','C++']; 问题:arr数组是否包含‘jav ...

  4. Tiny4412 Linux 内核启动流程

    Linux内核的启动分为压缩内核和非压缩内核两种,这里我们以压缩内核为例.压缩内核运行时,将运行一段解压缩程序,得到真正的内核镜像,然后跳转到内核镜像运行.此时,Linux进入非压缩内核入口,在非压缩 ...

  5. C语言学习笔记--多维数组和多维指针

    1. 指向指针的指针 (1)指针的本质是变量,会占用一定的内存空间 (2)可以定义指针的指针来保存指针变量的地址值 (3)指针是个变量,同样也存在传值调用与传址调用 重置动态空间的大小 #includ ...

  6. js兼容事件

    //浏览器检测(function () { window.sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.ma ...

  7. hadoop再次集群搭建(5)-CDH Install

       登录 http://node1.com:7180/.用户名和密码都是admin.启动服务命令是 service  cloudera-scm-server start 最开始两个页面直接conti ...

  8. Enumeration与Iterator的对比

    Enumeration与Iterator的对比 Enumeration 接口 Iterator 接口 参数的含义 枚举类型 迭代器元素类型 所在包 java.util 父类 无 子类 StringTo ...

  9. linux中怎么结果一页一页输出

    比如sudo apt-cache search php 结果可能有两个屏幕,但是命令执行完毕之后直接就跳到最后面了,我想看第一行怎么办? 可以用管道命令(|)把一个命令的结果作为另一个命令的参数即可. ...

  10. Python 使用其他邮件服务商的 SMTP 访问(QQ、网易、163、Google等)发送邮件

    163邮箱SMTP授权 使用Python SMTP发送邮件 # -*- coding:utf-8 -*- from __future__ import print_function __author_ ...