Spring学习一之IOC工作原理 4

上述几篇内容都没有涉及到Spring的内容,就是为了更好地阐述Spring的工作原理

 

如图:

 

使用Spring的框架,当然要编写appcontent.xml文件:

如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
	"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean name="fileHelloWorld" class="com.nantian.spring.example4.HelloWorld">
		<constructor-arg>
			<ref bean="fileHello"/>
		</constructor-arg>
	</bean>
	<bean name="fileHello" class="com.nantian.spring.example4.FileHelloStr">
		<constructor-arg>
			<value>helloworld.properties</value>
		</constructor-arg>
	</bean>
</beans>


如图:

 

再重构HelloWorldClient类:

如下:

/**
 * 
 */
package com.nantian.spring.example4;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.nantian.spring.example4.HelloWorld;

/**
 * @author ps
 *
 */
public class HelloWorldClient {
	protected static final Log log = LogFactory.getLog(HelloWorldClient.class);
	
	public HelloWorldClient() {
		Resource resource = new ClassPathResource("com/nantian/spring/example4/appcontent.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		
		HelloWorld helloWorld = (HelloWorld)factory.getBean("fileHelloWorld");
		log.info(helloWorld.getContent());
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new HelloWorldClient();
	}

}


最后运行HelloWorldClient类输出:

二月 09, 2012 3:55:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/nantian/spring/example4/appcontent.xml]
二月 09, 2012 3:55:24 下午 com.nantian.spring.example4.HelloWorldClient <init>
信息: "Hello World!"

 

开发者还注意到,上述输出结果同前面3个实例有所不同,除了输入本身的信息外,还包括了Spring的输出信息。

但结果是一样的。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值