分类:
2008-09-09 12:41:42
1. 创建项目
新建一个 Project:SpringBeanFile,注意要导入用户库Spring.
这是完成后整个项目的结构(预览一下):项目源码(不包含库文件):?javamxj 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");
}
}
[1]