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. poj 2749

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6091   Accepted: 2046 De ...

  2. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  3. Javascript scrollTop 20大洋

    花了20大洋,买了一个视频,这是读书笔记 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"&g ...

  4. selenium测试框架篇

    做自动化框架,不可避免的就是对象库. 有一个好的对象库,可以让整个测试体系: 更容易维护 大大增加代码重用 增加测试系统的稳定性 这里先了解一下我所说的对象库: 所谓的页面对象,是指每一个真是的页面是 ...

  5. Java:Comparator接口

    public interface Comparator<T> 接口里面的方法 int compare(T o1, T o2) o1 > o2 返回 1 o1 = o2 返回 0 o1 ...

  6. hdu 2964 Prime Bases(简单数学题)

    按照题意的要求逐渐求解: #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...

  7. iOS开发--动画篇之layout动画深入

    "不得不说,单单是文章的标题,可能不足以说明本文的内容.因此,在继续讲述约束动画之前,我先放上本文要实现的动画效果." 编辑:Bison投稿:Sindri的小巢 约束动画并不是非常 ...

  8. 【golang】用container/list实现栈(Stack)

    go语言中的container有heap.list.ring,没有stack. 其中heap是优先级队列,虽然有Push()/Pop()接口,但是使用heap要实现heap.Interface接口,不 ...

  9. Head First HTML5 Programming笔记--chapter1 认识HTML5

    升级到HTML5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 //EN" "http://www.w3.org/TR/ ...

  10. MyBatis学习总结_02_使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...