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读取配置文件的几种方式的更多相关文章

  1. 关于spring读取配置文件的两种方式

    很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...

  2. Spring 读取配置文件的俩种方式

    读取配置可通过 org.springframework.core.env.Environment 类来获取, 也可以通过@Value的方式来获取 注解形式: @PropertySource({&quo ...

  3. Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!

    在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面.所以, 今天完整的分享Spring Boot读取配置文件的几种方式! S ...

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

    Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...

  5. spring-boot-route(二)读取配置文件的几种方式

    Spring Boot提供了两种格式的配置文件,分别是properties 和 yml.Spring Boot最大的特点就是自动化配置,如果我们想修改自动化配置的默认值,就可以通过配置文件来指定自己服 ...

  6. Java中spring读取配置文件的几种方法

    Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...

  7. java 学习笔记 读取配置文件的三种方式

    package com.itheima.servlet.cfg; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...

  8. Servlet读取配置文件的三种方式

    一.利用ServletContext.getRealPath()[或getResourceAsStream()] 特点:读取应用中的任何文件.只能在web环境下. private void text3 ...

  9. spring boot中读取配置文件的两种方式

    application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...

随机推荐

  1. Guava官方文档-RateLimiter类

    转载自并发编程网 – ifeve.com RateLimiter 从概念上来讲,速率限制器会在可配置的速率下分配许可证.如果必要的话,每个acquire() 会阻塞当前线程直到许可证可用后获取该许可证 ...

  2. LA 4727

    Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. W ...

  3. UITextField中文搜索

    导入头文件 #import "ChineseInclude.h"#import "PinYinForObjc.h" NSMutableArray *search ...

  4. POJ 1458

    #include <iostream> #include <string> #define MAXN 1000 using namespace std; string s_1; ...

  5. SQL 比较乱

    --DROP TABLE T_UserInfo---------------------------------------------------- --建测试表 CREATE TABLE T_Us ...

  6. POJ 3034 Whac-a-Mole(DP)

    题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在( ...

  7. 【poj1006-biorhythms】中国剩余定理

    http://poj.org/problem?id=1006 题意:中国剩余定理的裸题. 题目可转化为求最小的x满足以下条件: x%23=a;x%28=b;x%33=c; 关于中国剩余定理可看我昨天的 ...

  8. [主席树]HDOJ2665 && POJ2104 && POJ2761

    主席树真是神奇的物种! Orz一篇资料 题意:给n.m   下面有n个数 (编号1到n) 有m个询问,询问的是上面的数的编号在[l,r]之间第k小的数 n.m的范围都是$10^5$ 是主席树的入门题 ...

  9. 安卓中bundle的使用

    Bundle类用作携带数据,它类似于Map,用于存放key-value形式的值,相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,Bundle的内部实际上是使用了HashMa ...

  10. 用httpPost对JSON发送和接收的例子

    HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json";         ...