腾飞工作室

Spring读取配置文件的几种方式

Spring读取配置文件的几种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
/**
* BeanFactory只有依赖注入功能没有AOP功能, ApplicationContext继承自BeanFactory有AOP功能
*/
public class GetBeanFactory {
/**
* 加载项目内的配置文件,读取classPath之下的文件
*/
public void test01() {
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(resource);
StudentAction studentService = (StudentAction) bf
.getBean("StudentAction");
System.out.println(studentService);
}
/**
* 加载项目外的配置文件,File读取C盘下的文件
*/
public void test02() {
Resource resource = new FileSystemResource("C:/applicationContext.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
StudentAction studentAction = (StudentAction) beanFactory
.getBean("studentAction");
System.out.println(studentAction);
}
/**
* 读取Tomcat中的application配置文件, 必须导入Spring3-Web.jar包
*/
public void test03() {
/*
* 将下面的代码必须放到jsp页面里面执行 <% org.springframework.core.io.Resource
* resource=null; org.springframework.beans.factory.BeanFactory
* beanFactory=null; resource=new
* org.springframework.web.context.support
* .ServletContextResource(application
* ,"/WEB-INF/classes/applicationContext.xml"); beanFactory=new
* org.springframework.beans.factory.xml.XmlBeanFactory(resource);
* System.out.println(beanFactory); %>
*/
}
/**
* ApplicationContext继承自BeanFactory有AOP功能
*/
public void test04() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
StudentService studentService = (StudentService) context
.getBean("studentService");
studentService.save(new Student("test", 22));
}
/**
* ApplicationContext继承自BeanFactory有AOP功能
*/
public void test05() {
ApplicationContext context = new FileSystemXmlApplicationContext(
"C:/applicationContext.xml");
System.out.println(context.getBeanDefinitionCount());// 定义bean的总数
}
/**
* ApplicationContext继承自BeanFactory有AOP功能
*/
public void test06() {
String[] filepath = { "applicationContext.xml" };
ApplicationContext factory = new ClassPathXmlApplicationContext(
filepath);
StudentService studentService = (StudentService) factory
.getBean("studentService");
}
/**
* 用Spring读取properties文件
*/
@Test
public void test07() throws Exception, Exception {
Resource r = new ClassPathResource("ssh.properties");
Properties p=new Properties();
p.load(new FileInputStream(r.getFile()));
System.out.println(p.get("studentDao"));
}
@Test
public void test08() throws Exception, Exception {
Resource r = new ClassPathResource("a.txt");
}
}

本文出自 “腾飞工作室” 博客,请务必保留此出处:http://tfgzs.net/2014/08/24/Spring读取配置文件的几种方式/