Spring读取配置文件的几种方式
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");
} }
Spring读取配置文件的几种方式的更多相关文章
- 关于spring读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
- Spring 读取配置文件的俩种方式
读取配置可通过 org.springframework.core.env.Environment 类来获取, 也可以通过@Value的方式来获取 注解形式: @PropertySource({&quo ...
- Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!
在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...
- Spring Boot读取配置文件的几种方式
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...
- spring-boot-route(二)读取配置文件的几种方式
Spring Boot提供了两种格式的配置文件,分别是properties 和 yml.Spring Boot最大的特点就是自动化配置,如果我们想修改自动化配置的默认值,就可以通过配置文件来指定自己服 ...
- Java中spring读取配置文件的几种方法
Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...
- java 学习笔记 读取配置文件的三种方式
package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...
- Servlet读取配置文件的三种方式
一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...
- spring boot中读取配置文件的两种方式
application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...
随机推荐
- NSArray block用法
28.使用block 块遍历整个数组.这个block 需要三个参数,id obj 表示数组中的元素. NSUInteger idx 标示元素的下标, bool *stop 是一个bool类型的参数. ...
- Masonry自动布局
介绍,入门: http://www.cocoachina.com/ios/20141219/10702.html 下载: http://code.cocoachina.com/detail/30114 ...
- hadoop,spark,linux上常用命令
记下常用命令,慢慢补充 1.hadoop 查看hdfs上的目录: hadoop fs -ls /给hdfs上目录授予权限: hadoop fs -chmod 777 /tmp/hive 在hdfs ...
- C# winform post请求数据
刚到公司混的时候,老板要求实现一个从C#的windows应用程序传参数到一个网页,然后这个网页不显示出来,但能把数据返回给应用程序的功能,问了好多人,找了好多资料,都搞不定,后来还是在老板的帮助下搞定 ...
- 李洪强iOS开发之OC[010] - 有参方法的声明实现和调用
// // main.m // 09 - 有参方法的声明实现和调用 // // Created by vic fan on 16/7/5. // Copyright © 2016年 李洪强. ...
- lintcode:验证二叉查找树
题目 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个 ...
- 查看服务器硬件配置信息(cpu/内存)
1.查看cpu情况: 方法一: Linux下CPU相关的参数保存在 /proc/cpuinfo 文件里 cat /proc/cpuinfo |more 方法二: 采用命令 dmesg ...
- ios开发解决遍历删除数组对象报错
// 删除对应数据 // for (OrderModel *order in self.OrderList) { // // ...
- 生产环境的redis高可用集群搭建
这里只是总结一下安装步骤 如果要了解redis集群高可用的原理,推荐仔细看一遍配置文件示例http://download.redis.io/redis-stable/redis.conf,源码包里也有 ...
- spring路径通配符
在应用Spring的工程中,使用class path的方式加载配置文件应该是最常用的做法,然而对大部分人来说,刚开始使用Spring时,几乎都碰到过加载配置文件失败的情况,除了配置上的错误外,很多时候 ...