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 ...
随机推荐
- 大一暑假为期五周的ACM实验室培训结束了(2013.8.24)
没想到,我的大学里第一个暑假,9周的时间只有最初的两周在家待着,接下来的7周将会在学校度过. 说真的,这是我上学以来,第一次真正好好利用的假期.在这五周里,周一.三.五下午学长都会给我们讲点知识,之后 ...
- poj 3468(线段树+lazy思想)
题目链接:http://poj.org/problem?id=3468 思路:如果直接去做,每次都更新到叶子节点,那必然会TLE,我们可以采用lazy的思想:没必要每次更新都更新到叶子节点,只要有一个 ...
- Javascript 数组循环遍历之forEach
1. js 数组循环遍历. 数组循环变量,最先想到的就是 for(var i=0;i<count;i++)这样的方式了. 除此之外,也可以使用较简便的forEach 方式 2. forE ...
- 基础DOM和CSS操作(三)
CSS方法 jQuery不但提供了CSS的核心操作方法,比如.css()..addClass()等.还封装了一些特殊功能的CSS操作方法,我们分别来了解一下. width()方法 方法名 描述 wid ...
- IDA 使用技巧
我用的IDA Pro 6.5,把我自己使用ida的一些方法记录,免得自己遗忘 1 .导入符号表 可以像前一篇博客中写的那样,也可以使用File--->LoadFile--->PDB Fil ...
- Log4net学习
转自:http://www.cnblogs.com/sirkevin/archive/2012/06/13/2548449.html Log4net简介根据日志类别保存到不同的文件,并按照日期生成不同 ...
- sql openrowset
select * from openrowset('sqloledb','ip';'user';'pwd','exec 库..过程')
- 欧拉工程第55题:Lychrel numbers
package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; import java.util ...
- HTML5的fieldset标签
定义和用法 fieldset 元素可将表单内的相关元素分组,绘制一个带标题的框,有如winform开发中的panel. <fieldset> 标签将表单内容的一部分打包,生成一组相关表单的 ...
- 295. Find Median from Data Stream
题目: Median is the middle value in an ordered integer list. If the size of the list is even, there is ...