`
CoderDream
  • 浏览: 470756 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

【精通Spring(第一版)】笔记:第一章 Spring启程

阅读更多

【实例1】example01:

FileHelloStr类

package com.coderdream.spring;

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

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

/**
 * 基于文件形式,读取 HelloWorld 所需的字符串
 * 
 * @author CoderDream
 *
 */
public class FileHelloStr {
	protected static final Log log = LogFactory.getLog(FileHelloStr.class);

	private String propfilename;

	public FileHelloStr(String propfilename) {
		this.propfilename = propfilename;
	}

	public String getContent() {
		String helloworld = "";

		try {
			Properties prop = new Properties();
			InputStream is = getClass().getClassLoader().getResourceAsStream(
					propfilename);

			prop.load(is);

			is.close();
			helloworld = prop.getProperty("helloworld");

		} catch (FileNotFoundException ex) {
			log.error(ex);
		} catch (IOException ex) {
			log.error(ex);
		}

		return helloworld;
	}

}

 HelloWorld类:

package com.coderdream.spring;

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

/**
 * 获得 HelloWorld 字符串
 * 
 * @author CoderDream
 *
 */
public class HelloWorld {
	protected static final Log log = LogFactory.getLog(HelloWorld.class);

	public String getContent() {
		FileHelloStr fhStr = new FileHelloStr("helloworld.properties");

		String helloworld = fhStr.getContent();

		return helloworld;
	}

}

HelloWorldClient类,实现对HelloWorld类的调用:

package com.coderdream.spring;

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

/**
 * HelloWorld 客户应用
 * 
 * @author CoderDream
 *
 */
public class HelloWorldClient {
	
	protected static final Log log = LogFactory.getLog(HelloWorldClient.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelloWorld hw = new HelloWorld();
		log.info(hw.getContent());
	}

}

 HelloWorld.properties文件:

helloworld="Hello World!"

 build.xml:(注意,如果工程为UTF-8,则build.xml的编码格式要设置为UTF-8,否则会以本地格式编译,如果为繁体中文OS,则编码格式为MS950,如果Java文件中有简体中文字符,则编译会出错! )

<?xml version="1.0" encoding="UTF-8" ?>
<project name="example01" default="run" basedir=".">
	<path id="lib">
		<fileset dir="F:/Java/MasterSpringLib">
			<include name="commons-logging-1.1.1.jar" />
		</fileset>
	</path>
	<target name="run" depends="compile" description="Run HelloWorldClient">
		<java classname="com.coderdream.spring.HelloWorldClient" fork="yes">
			<classpath refid="lib" />
			<classpath path="classes" />
		</java>
	</target>

	<target name="compile">
		<mkdir dir="classes" />

		<javac destdir="classes" source="1.5" target="1.5" deprecation="false" optimize="false" failonerror="true">
			<src path="src" />
			<classpath refid="lib" />
		</javac>

		<copy todir="classes">
			<fileset dir="src">
				<include name="HelloWorld.properties" />
			</fileset>
		</copy>

	</target>
</project>

 通过Ant build.xml 文件的 run 任务,输出如下信息:

Buildfile: C:\Documents and Settings\XL\workspace\example01\build.xml
compile:
    [javac] Compiling 1 source file to C:\Documents and Settings\XL\workspace\example01\classes
run:
     [java] 2008/10/13 下午 03:50:32 com.coderdream.spring.HelloWorldClient main
     [java] 資訊: "Hello World!"
BUILD SUCCESSFUL
Total time: 1 second

【实例2】example02

对“实例1”进行重构:

【1】、创建 HelloStr 接口,该接口包含getContent()方法的声明;

【2】、类 FileHelloStr 实现 HelloStr 接口;

【3】、重构 HelloWorld 类;

public class HelloWorld {
	protected static final Log log = LogFactory.getLog(HelloWorld.class);
	
	private HelloStr hStr;
	
	public HelloWorld(HelloStr hStr) {
		this.hStr = hStr;
	}

	public String getContent() {		
		return hStr.getContent();
	}



}

【4】、重构 HelloWorldClient 客户应用;

public class HelloWorldClient {
	
	protected static final Log log = LogFactory.getLog(HelloWorldClient.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FileHelloStr fhStr = new FileHelloStr("HelloWorld.properties");
		HelloWorld hw = new HelloWorld(fhStr);


		log.info(hw.getContent());
	}

}

 【实例3】:example03

在“实例2”的基础上完成如下重构:

【1】、创建 HelloWorldFactory 工厂:

package com.coderdream.spring;

/**
 *  注入 HelloWorld 和 HelloStr 依赖性
 * 
 * @author CoderDream
 *
 */
public class HelloWorldFactory {
	public static HelloWorld getFileHelloWorld() {
		HelloStr hStr = new FileHelloStr("HelloWorld.properties");
		HelloWorld hw = new HelloWorld(hStr);
		
		return hw;
	}
}

 【2】重构 HelloWorldClient 客户应用;

public class HelloWorldClient {
	
	protected static final Log log = LogFactory.getLog(HelloWorldClient.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		HelloWorld hw = HelloWorldFactory.getFileHelloWorld();


		log.info(hw.getContent());
	}

}

 其中,工厂负责创建和集成客户应用所需的对象,至此,通过借助于依赖注入(工厂类)实现控制反转。

 

【实例4】example04

在“实例3”的基础上完成如下重构:

【1】增加 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.coderdream.spring.HelloWorld">
		<constructor-arg>
			<ref bean="fileHello" />
		</constructor-arg>
	</bean>
	<bean name="fileHello" class="com.coderdream.spring.FileHelloStr">
		<constructor-arg>
			<value>HelloWorld.properties</value>
		</constructor-arg>
	</bean>

</beans>

 【2】重构 HelloWorldClient 客户应用:

public class HelloWorldClient {
	
	protected static final Log log = LogFactory.getLog(HelloWorldClient.class);

	public HelloWorldClient() {
		Resource resource = new ClassPathResource("appcontext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		HelloWorld hw = (HelloWorld)factory.getBean("fileHelloWorld");
		log.info(hw.getContent());
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		new HelloWorldClient();
	}



}

 【3】更新build.xml中的内容:

<?xml version="1.0" encoding="UTF-8"?>
<project name="example01" default="run" basedir=".">
	<path id="lib">
		<fileset dir="F:/Java/MasterSpringLib">
			<include name="commons-logging-1.1.1.jar" />
			<include name="spring.jar" />


		</fileset>
	</path>
	<target name="run" depends="compile" description="Run HelloWorldClient">
		<java classname="com.coderdream.spring.HelloWorldClient" fork="yes">
			<classpath refid="lib" />
			<classpath path="classes" />
		</java>
	</target>

	<target name="compile">
		<mkdir dir="classes" />

		<javac destdir="classes" source="1.5" target="1.5" deprecation="false" 
			optimize="false" failonerror="true">
			<src path="src" />
			<classpath refid="lib" />
		</javac>

		<copy todir="classes">
			<fileset dir="src">
				<include name="HelloWorld.properties" />
				<include name="appcontext.xml" />


			</fileset>
		</copy>

	</target>
</project>

 运行结果:

Buildfile: C:\Documents and Settings\XL\workspace\example04\build.xml
compile:
run:
     [java] 2008/10/13 下午 04:06:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
     [java] 資訊: Loading XML bean definitions from class path resource [appcontext.xml]
     [java] 2008/10/13 下午 04:06:52 org.springframework.beans.factory.support.AbstractBeanFactory getBean
     [java] 資訊: Creating shared instance of singleton bean 'fileHelloWorld'
     [java] 2008/10/13 下午 04:06:52 org.springframework.beans.factory.support.AbstractBeanFactory getBean
     [java] 資訊: Creating shared instance of singleton bean 'fileHello'
     [java] 2008/10/13 下午 04:06:52 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory autowireConstructor
     [java] 資訊: Bean 'fileHello' instantiated via constructor [public com.coderdream.spring.FileHelloStr(java.lang.String)]
     [java] 2008/10/13 下午 04:06:52 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory autowireConstructor
     [java] 資訊: Bean 'fileHelloWorld' instantiated via constructor [public com.coderdream.spring.HelloWorld(com.coderdream.spring.HelloStr)]
     [java] 2008/10/13 下午 04:06:52 com.coderdream.spring.HelloWorldClient <init>
     [java] 資訊: "Hello World!"
BUILD SUCCESSFUL
Total time: 891 milliseconds
 

 通过Spring输出信息可知,工程创建了两个单实例的HelloWorld 和 FileHelloStr,即 Spring 默认时仅创建单实例的 JavaBean,通过 Spring 配置文件中 bean 元素的 singleton 属性能够控制创建 Java 实例的方式。

 

 

源代码运行:

在F盘新增F:\Java\MasterSpringLib文件夹,将commons-logging-1.1.1.jar和spring.jar拷贝到此目录中。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics