Spring学习一之IOC工作原理
来源:互联网 发布:淘宝卖家如何玩转微淘 编辑:程序博客网 时间:2024/05/18 02:53
为阐述清楚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.xmlcompile: [javac] Compiling 3 source files to D:\work\j2ee\workspace\spring1\src\com\nantian\spring\example1\classesrun: [java] 二月 09, 2012 3:28:41 下午 com.nantian.spring.example1.HelloWorldClient main [java] 信息: "Hello World!"BUILD SUCCESSFULTotal time: 1 second
上述过程中,开发者可以看出:HelloWorld明显依赖FileHelloStr,如果开发者需要通过其他途径获取“HelloWorld”信息,则需要重构现有的FileHelloStr类,即通过更通用的HelloStr接口形式给出。一种较好的实现方式是将创建FileHelloStr对象的职责委派给HelloWorldClient客户。
呵呵见下篇。
- Spring学习一之IOC工作原理
- Spring学习一之IOC工作原理 2
- Spring学习一之IOC工作原理 3
- Spring学习一之IOC工作原理 4
- Spring IOC 工作原理
- spring学习日志之四 IOC原理
- spring ioc,aop工作原理
- spring之ioc原理
- Spring之IoC原理
- Spring框架学习之IOC(一)
- JAVAEE之Spring学习(一)-----ioc容器
- Spring Ioc学习(一)
- Spring 学习之 IOC
- spring学习之Ioc
- Spring学习之IOC
- Spring之IOC学习
- Spring学习之IOC
- 【Spring学习】之 IOC
- Windows xp系统硬盘安装方案
- Oracle中connect by...start with...的使用
- RTMP协议分析
- 上传图片并给图片加水印(公共类积累)
- 对重复数据删除的一些误解(一)
- Spring学习一之IOC工作原理
- Vector容器的一点说明
- SQL Server逗号分隔字符串拆成临时表
- 榨干 PHP,不得不转的一篇PHP使用技巧!
- 使用Maven的antrun插件启动Tomcat的两种方法
- android GPS定位和卫星个数(源码)
- Counting Objects in C++
- poj 1056 判断前缀
- c++中vector排序的问题二(转载)