Spring:加载xml配置文件

  Spring容器中Ioc容器也称为bean 工厂,负责创建bean 实例,并管理了bean生命周期。

Spring容器最基本的接口是BeanFactory,该接口提供了高级的配置机制来管理、实例化Bean,spring中所有的配置文件的类都直接或者间接地实现了该接口;BeanFactory有很多子接口,最重要的就是ApplicationContext接口。

1、Bean的实例化:

当通过BeanFactory.getBean(..)获取bean时,会经过一下步骤:

  ①调用bean的构造方法,创建bean实例

  ②如果配置文件中有属性值注入,则注入属性值

  ③如果bean实现了InitializingBean接口,则调用afterPropertiesSet()方法

  ④如果在配置文件中设置了init-method=‘xxxx’属性,则调用xxx方法初始化操作

2、加载xml配置文件

(1)使用BeanFactory接口FileSystemResource类通过系统路径加载配置文件:(可以是绝对路径,也可以是相对路径)

// 系统路径加载
		String sysPath = SpringTest.class.getResource("").toString();
		int pos_sys = sysPath.indexOf(":");
		sysPath = sysPath.substring(pos_sys+2);
		Resource resourceSyetem = new FileSystemResource(sysPath+"beans-config.xml");
		BeanFactory factory = new XmlBeanFactory(resourceSyetem);
		MessageBean messageBean_3 = (MessageBean)factory.getBean("messageBean");
		System.out.println("-->"+messageBean_3.getMessage());
 (2)使用BeanFactory接口ClassPathResource 类通过classpath路径查找配置文件并加载:

                URL urlAb = SpringTest.class.getResource("");
		URL urlSt = SpringTest.class.getResource("/");
		String path = urlAb.toString().replace(urlSt.toString(), "");
		
		System.out.println("path->"+path);
		// 创建ClassPathResource对象,类加载器将在classpath中查找beans-config.xml文件.
		ClassPathResource resource = new ClassPathResource(path+"beans-config.xml");
		
		/*spring 根据beans-config.xml文件中的配置信息创建BeanFactory的实例,
		     同时完成bean的创建和属性值得注入。*/
		BeanFactory beanFactory = new XmlBeanFactory(resource);
		
		// getBean() 取得bean实例
		MessageBean bean = (MessageBean)beanFactory.getBean("messageBean");//从配置文件中
		System.out.println(bean.getMessage());
		

(3)使用ApplicationContext接口FileSystemXmlApplicationContext类通过系统路径加载配置文件:(可以是绝对路径,也可以是相对路径)

                ApplicationContext ac_ = new FileSystemXmlApplicationContext(sysPath+"beans-config.xml");
		MessageBean messageBean_4 = (MessageBean)ac_.getBean("messageBean");
		System.out.println("4-->"+messageBean_4.getMessage());

 (4) 使用ApplicationContext接口ClassPathXmlApplicationContext 类通过classpath路径查找配置文件并加载:(可加载多个配置文件 )

	       URL urlAb = SpringTest.class.getResource("");
		URL urlSt = SpringTest.class.getResource("/");
		String path = urlAb.toString().replace(urlSt.toString(), "");
		ApplicationContext ac = new ClassPathXmlApplicationContext( new String[]{path+"beans-config.xml"});
		MessageBean messageBean_2 = (MessageBean)ac.getBean("messageBean");
		System.out.println(messageBean_2.getMessage());

(5)XmlWebApplicationContext:从Web系统中的XML文件载入上下文定义信息。

        @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPost(request, response);
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		MessageBean message = (MessageBean)ac.getBean("messageBean");
		out.println(message.getMessage());
		ComBean comBean = (ComBean)ac.getBean("comBean");
		out.println(comBean.getMessage());
		out.println(comBean.getRefBean().getInfo());
		MethodBean method = (MethodBean)ac.getBean("methodBean");
		out.println(method.getName());
		out.println(method.getRefBean().getInfo());
		out.close();//close
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值