1. 创建项目
· 新建一个Java Project:SpringBeanFile,注意要导入用户库Spring。
· 这是完成后整个项目的结构(预览一下):
2. 编写类文件
· 下面开始创建一个新类:BeanFile ;包名:javamxj.spring.beanfile
BeanFile.java
|
package javamxj.spring.beanfile;
public class BeanFile {
private String beanFile = "多种方式加载Bean的配置文件";
public void setBeanFile(String beanFile) { this.beanFile = beanFile; }
public String getBeanFile() { return beanFile; } }
|
· 新建Test.java,测试一下。
package javamxj.spring.beanfile;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
// 直接调用HelloBean
BeanFile bf = new BeanFile();
System.out.println(bf.getBeanFile());
/**
* 利用XmlBeanFactory(Resource resource)
* 这里Resource必须是xml格式
* Resource包括:AbstractResource, ClassPathResource, FileSystemResource,
* InputStreamResource, ServletContextResource, UrlResource
*/
/*
* 利用 InputStreamResource(InputStream inputStream)
* 要将bean.xml放在项目根目录下
*/
InputStream is = null;
try {
is = new FileInputStream("bean1.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Resource resource = new InputStreamResource(is);
sayHello(resource);
/*
* 利用 ClassPathResource(String path)
* 要将bean.xml放在源文件夹(src)目录下
*/
resource = new ClassPathResource("bean2.xml");
sayHello(resource);
/*
* 利用 FileSystemResource(String path)
* 要将bean.xml放在项目根目录下
*/
resource = new FileSystemResource("bean3.xml");
sayHello(resource);
/*
* 利用 Properties
* 要将bean.properties放在类路径--源文件夹(src)目录下
*/
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(
reg);
reader.loadBeanDefinitions(new ClassPathResource("bean.properties"));
BeanFactory factory = (BeanFactory) reg;
bf = (BeanFile) factory.getBean("beanFile");
System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.properties");
/*
* 利用 ApplicationContext
* 要将bean.xml放在类路径--源文件夹(src)目录下
*/
ApplicationContext appContext = new ClassPathXmlApplicationContext(
"bean4.xml");
bf = (BeanFile) appContext.getBean("beanFile");
System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.xml");
}
public static void sayHello(Resource resource) {
BeanFactory factory = new XmlBeanFactory(resource);
BeanFile bf = (BeanFile) factory.getBean("beanFile");
System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.xml");
}
}
3. 配置文件
由上面的Test.java可知,这里一共需要四个XML文件和一个Properties文件,现在分别建立。
· bean1.xml放在项目根目录下:
bean1.xml
|
<?xml version="1.0" encoding="GBK"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <bean id="beanFile" class="javamxj.spring.beanfile.BeanFile"> <property name="beanFile"> <value>InputStreamResource(InputStream inputStream)</value> </property> </bean> </beans>
|
bean2.xml、bean3.xml、bean4.xml与bean1.xml相似,仅仅需要替换一下<value>值即可。重要的注意文件的存放位置。这里只给出不同的代码;
· bean2.xml放在源文件夹(src)目录下:
bean2.xml(部分)
|
<property name="beanFile"> <value>ClassPathResource(String path)</value> </property>
|
· bean3.xml放在项目根目录下:
bean3.xml(部分)
|
<property name="beanFile"> <value>FileSystemResource(String path)</value> </property>
|
· bean4.xml放在源文件夹(src)目录下:
bean4.xml(部分)
|
<property name="beanFile"> <value>ApplicationContext</value> </property>
|
Spring也可以使用属性文件来定义配置文件,如下:
· bean.properties放在源文件夹(src)目录下:
bean.properties
|
beanFile.class=javamxj.spring.beanfile.BeanFile beanFile.beanFile=properties
|
4. 运行程序
右击Test.java,运行程序,控制台输出如下:
多种方式加载Bean的配置文件
利用 InputStreamResource(InputStream inputStream) 加载 Bean.xml
利用 ClassPathResource(String path) 加载 Bean.xml
利用 FileSystemResource(String path) 加载 Bean.xml
利用 properties 加载 Bean.properties
利用 ApplicationContext 加载 Bean.xml
5. 小结
这篇文章主要谈论了如何加载Spring的配置文件,一般来说,就是BeanFactory和ApplicationContext。最常使用的、简单的BeanFactory实现是org.springframework.beans.factory.xml.XmlBeanFactory,其加载方式为:
BeanFactory factory = new XmlBeanFactory(Resource resource)
这里resource必须是xml格式。Resource包括: AbstractResource, ClassPathResource, FileSystemResource, InputStreamResource, ServletContextResource, UrlResource。这篇文章谈了常用的三种:ClassPathResource, FileSystemResource, InputStreamResource。
ApplicationContext包括了BeanFactory的所有功能,也要比BeanFactory强大的多(以后会详细介绍的)。这里只简单的使用了ClassPathXmlApplicationContext加载了Bean配置文件。你可以将log4j.properties中的“Warn”改为“Debug”,对比一下和ClassPathResource的输出,
在Eclipse中,bean2.xml、bean4xml虽然都是放在源文件夹(src)目录下,但实际上,是由已经编译好的Test.class从类文件夹(这里是bin文件夹)中加载的。
分享到:
相关推荐
英飞凌tc26x快速入门,TASKING教程,tc26x系列的单片机
Ucinet软件快速入门上手-网络分析软件.pdf
Origin8.0简明教程--快速上手.pdf )Origin8.0简明教程--快速上手.pdf )Origin8.0简明教程--快速上手.pdf )
Ucinet软件快速入门上手-网络分析软件.doc
wine,可以快速使用的工具,可以上手试一试
Spire.Doc-FE_2.7.3.jar,是一款java生成word的工具类,可以很方便的提供word的各种各样的操作,还有在线说明文档 ...基本操作都有java demo,所以很好上手。
我们对Spring平台和第三方库进行了一种自以为是的观点,这样您就可以轻松上手了。大多数Spring Boot应用程序只需要很少的Spring配置。 您可以使用Spring Boot创建可以使用java -jar或更传统的war部署启动的Java应用...
WinSCP-5.11.2.7781p.zip ,简单方便,中文汉化,新手上手快
005 - Spark框架 - 快速上手 - WordCount - 案例分析.avi 009 - Spark框架 - 快速上手 - WordCount - Spark的实现.avi 010 - Spark框架 - 快速上手 - WordCount - 日志和错误.avi 011 - Spark框架 - 运行环境 - 本地...
JDOM处理XML快速上手 - Web UI Design - JavaEye技术网站.mht
项目一--Photoshop-CS6快速上手.ppt
简单的学习项目 适合刚上手的小白 有问题欢迎交流
Apache Avro 是类似于 Google protobuf 那样的数据交换协议,但 Avro 可以用 JSON 格式来定义 Schema, 所以相比而言更容易上手。它也是 Hadoop, Kafka 所采用的交换格式。对于生成的 avro 序列化文件如果要编写代码...
ApacheFTPServer,搭建环境方便快捷,快速上手
此外,它还具有良好的用户界面和易用的导航菜单,使得用户可以快速上手并轻松地进行项目管理。由于其源代码开放,因此学生可以根据自己的需求进行定制和扩展,以实现更加个性化的功能。总之,JavaPMS-1.2-beta.zip是...
非常棒的一套Spring实战课程,课程由经验非常丰富的架构师带领同学们,手把手的学习Spring技术,同时课程适合各个阶段的人群进行学习,非常的不错。 视频大小:4.5G
JqueryMobile快速上手教程,教学资源,拿来就用,可以上课作为任务书,也可以作为自学习文档
Greenplum是一个面向数据仓库应用的关系型数据库,它基于流行的PostgreSQL开发,因为有良好的体系结构,所以在数据存储,高并发,高可用,线性...在功能和语法上,要比Hadoop上的SQL引擎Hive好用很多,普通用户更加容易上手。
包含:Ajax中文手册(快速上手).chm---CSS.chm---Hibernate_3.2.0_Reference_zh_CN.chm---J2EE完全参考手册-J2EE概述.pdf----Javascript高级教程.CHM---javaWeb.chm--JBPM工作流开发指南 .pdf---JSF中文教程.chm---JSP...