Spring学习一之IOC工作原理

本文通过具体示例展示了Spring IOC框架如何帮助开发人员管理对象之间的依赖关系。通过创建FileHelloStr类来读取属性文件,并在HelloWorld类中使用该功能,最后由HelloWorldClient进行调用来展示IOC的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为阐述清楚Spring IOC框架帮助开发人员完成了那些工作 ,因此从如下实例看:

example 1:

首先实现了FileHelloStr.java:

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

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author ps
 *
 */
public class FileHelloStr {
	protected static final Log log = LogFactory.getLog(FileHelloStr.class);
	private String profilename;
	
	public FileHelloStr(String profilename) {
		this.profilename = profilename;
	}
	
	/**
	 * 读取属性文件
	 * @return 返回helloworld对应的值
	 */
	public String getContent(){
		String helloWorld = "";
		
		try{
			Properties properties = new Properties();
			InputStream is = getClass().getClassLoader().getResourceAsStream(profilename);
			properties.load(is);
			is.close();
			helloWorld = properties.getProperty("helloworld");
		}catch (Exception e) {
			log.error(e.getMessage());
		}
		
		return helloWorld;
	}
}


上述类实现对传入的、名为profilename的属性文件的读取工作,并能够打印出相应异常信息。

其次开发者还需要实现HelloWorld类:

如下:

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author ps
 *
 */
public class HelloWorld {
	protected static final Log log = LogFactory.getLog(HelloWorld.class);
	
	/**
	 * 读取属性文件
	 * @return 返回helloworld对应的值
	 */
	public String getContent(){
		FileHelloStr helloStr = new FileHelloStr("helloworld.properties");
		
		return helloStr.getContent();
	}
}


上述类调用了FileHelloStr类

开发者还需要实现HelloWorldClient类,从而实现对HelloWorld的调用。

如下:

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author ps
 *
 */
public class HelloWorldClient {
	protected static final Log log = LogFactory.getLog(HelloWorldClient.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelloWorld helloWorld = new HelloWorld();
		
		log.info(helloWorld.getContent());
	}

}


然后开发者需要编写属性文件(helloworld.properties)其内容如下:

helloworld = "Hello World!"

编写Ant文件运行HelloWorldClient类。

Ant文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="example1" default="run" basedir=".">
	<path id="lib">
		<fileset dir="d:\learn\框架\struts-2.2.1.1\lib">
			<include name="commons-logging-1.0.4.jar" />
		</fileset>
	</path>
	
	<target name="run" depends="compile" description="Run HelloWorldClient">
		<java classname="com.nantian.spring.example1.HelloWorldClient" fork="yes">
			<classpath refid="lib"/>
			<classpath path="classes"/>
		</java>
	</target>
	
	<target name="compile">
		<mkdir dir="classes"/>
		
		<javac destdir="classes" source="1.7" target="1.7" 
		deprecation="false" optimize="false" failonerror="true"
		includeantruntime="false">
			<src path="."/>
			<classpath refid="lib"/>
		</javac>
		
		<copy todir="classes">
			<fileset dir="../../../../">
				<include name="helloworld.properties" />
			</fileset>
		</copy>
	</target>
</project>


运行结果:

Buildfile: D:\work\j2ee\workspace\spring1\src\com\nantian\spring\example1\ant.xml
compile:
    [javac] Compiling 3 source files to D:\work\j2ee\workspace\spring1\src\com\nantian\spring\example1\classes
run:
     [java] 二月 09, 2012 3:28:41 下午 com.nantian.spring.example1.HelloWorldClient main
     [java] 信息: "Hello World!"
BUILD SUCCESSFUL
Total time: 1 second


上述过程中,开发者可以看出:HelloWorld明显依赖FileHelloStr,如果开发者需要通过其他途径获取“HelloWorld”信息,则需要重构现有的FileHelloStr类,即通过更通用的HelloStr接口形式给出。一种较好的实现方式是将创建FileHelloStr对象的职责委派给HelloWorldClient客户。

呵呵见下篇。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值