首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

装配和使用SpringIDE-III

2012-07-01 
安装和使用SpringIDE-------IIIBeanFile.javapackagejavamxj.spring.beanfilepublicclassBeanFile {??? p

安装和使用SpringIDE-------III

BeanFile.java

packagejavamxj.spring.beanfile;

publicclassBeanFile {

??? privateStringbeanFile = "多种方式加载Bean的配置文件";

??? publicvoidsetBeanFile(String beanFile){
???????this.beanFile =beanFile;
??? }

??? publicStringgetBeanFile() {
???????returnbeanFile;
??? }
}?·新建Test.java,测试一下。

Test.java

packagejavamxj.spring.beanfile;

importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.InputStream;

importorg.springframework.beans.factory.BeanFactory;
importorg.springframework.beans.factory.support.BeanDefinitionRegistry;
importorg.springframework.beans.factory.support.DefaultListableBeanFactory;
importorg.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
importorg.springframework.beans.factory.xml.XmlBeanFactory;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.core.io.ClassPathResource;
importorg.springframework.core.io.FileSystemResource;
importorg.springframework.core.io.InputStreamResource;
importorg.springframework.core.io.Resource;

publicclassTest {
??? publicstaticvoidmain(String[] args){

???????// 直接调用HelloBean
???????BeanFile bf = newBeanFile();
???????System.out.println(bf.getBeanFile());

???????

???????
???????InputStream is =null;
???????try{
???????????is = new FileInputStream("bean1.xml");
???????} catch(FileNotFoundExceptione) {
???????????e.printStackTrace();
???????}
???????Resource resource = newInputStreamResource(is);
???????sayHello(resource);

???????
???????resource = newClassPathResource("bean2.xml");
???????sayHello(resource);

???????
???????resource = newFileSystemResource("bean3.xml");
???????sayHello(resource);

???????
???????BeanDefinitionRegistry reg = newDefaultListableBeanFactory();
???????PropertiesBeanDefinitionReader reader = newPropertiesBeanDefinitionReader(
???????????????reg);
???????reader.loadBeanDefinitions(newClassPathResource("bean.properties"));
???????BeanFactory factory = (BeanFactory) reg;

???????bf = (BeanFile) factory.getBean("beanFile");
???????System.out.println("利用" + bf.getBeanFile() + "加载 Bean.properties");
???????

???????
???????ApplicationContext appContext = newClassPathXmlApplicationContext(
???????????????"bean4.xml");
???????bf = (BeanFile) appContext.getBean("beanFile");
???????System.out.println("利用" + bf.getBeanFile() + "加载 Bean.xml");

??? }

??? publicstaticvoidsayHello(Resource resource) {
???????BeanFactory factory = newXmlBeanFactory(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/DTDBEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
??? <bean id="beanFile" class="javamxj.spring.beanfile.BeanFile">
???????<property name="beanFile">
???????????<value>InputStreamResource(InputStreaminputStream)</value>
???????</property>
??? </bean>
</beans>?bean2.xml、bean3.xml、bean4.xml与bean1.xml相似,仅仅需要替换一下<value>值即可。重要的注意文件的存放位置。这里只给出不同的代码;?·bean2.xml放在源文件夹(src)目录下:

bean2.xml(部分)

<property name="beanFile">
?????<value>ClassPathResource(Stringpath)</value>
</property>?·bean3.xml放在项目根目录下:

bean3.xml(部分)

<property name="beanFile">
?????<value>FileSystemResource(Stringpath)</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??·还需要将上文《快速上手Spring--2.HelloWorld(2)》中的log4j.properties复制到src目录下。??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 = newXmlBeanFactory(Resourceresource)这里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文件夹)中加载的。

热点排行