分类: 【java】2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报

1.简介

在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路径下,Spring的配置文件也是src/applicationContext.xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后注入Bean中。我们在程序中,可以根据Bean的Id,获取注入的值。这样我们就可以借助Spring来读取配置文件。

2.Code

2.1Student.java

  1. package edu.njupt.zhb.model.mysql;
  2. /**
  3. * Student entity. @author MyEclipse Persistence Tools
  4. */
  5. public class Student  implements java.io.Serializable {
  6. // Fields
  7. private String id;
  8. private String name;
  9. private String course;
  10. private Integer score;
  11. private String remarks;
  12. // Constructors
  13. /** default constructor */
  14. public Student() {
  15. }
  16. /** minimal constructor */
  17. public Student(String name, String course, Integer score) {
  18. this.name = name;
  19. this.course = course;
  20. this.score = score;
  21. }
  22. /** full constructor */
  23. public Student(String name, String course, Integer score, String remarks) {
  24. this.name = name;
  25. this.course = course;
  26. this.score = score;
  27. this.remarks = remarks;
  28. }
  29. // Property accessors
  30. public String getId() {
  31. return this.id;
  32. }
  33. public void setId(String id) {
  34. this.id = id;
  35. }
  36. public String getName() {
  37. return this.name;
  38. }
  39. public void setName(String name) {
  40. this.name = name;
  41. }
  42. public String getCourse() {
  43. return this.course;
  44. }
  45. public void setCourse(String course) {
  46. this.course = course;
  47. }
  48. public Integer getScore() {
  49. return this.score;
  50. }
  51. public void setScore(Integer score) {
  52. this.score = score;
  53. }
  54. public String getRemarks() {
  55. return this.remarks;
  56. }
  57. public void setRemarks(String remarks) {
  58. this.remarks = remarks;
  59. }
  60. }

2.2datasource.properties中的配置

  1. #student config
  2. student.name=Haibo
  3. student.id=1012010638
  4. student.course=Java
  5. student.score=90
  6. student.remarks=Come from Properties

2.3Spring配置文件applicationContext.xml部分配置

  1. <!-- 引入datasource配置文件 -->
  2. <context:property-placeholder location="classpath:datasource.properties" />
  3. <bean id="student" class="edu.njupt.zhb.model.mysql.Student">
  4. <property name="id" value="${student.id}" />
  5. <property name="name" value="${student.name}" />
  6. <property name="course" value="${student.course}" />
  7. <property name="score" value="${student.score}" />
  8. <property name="remarks" value="${student.remarks}" />
  9. </bean>

2.4读取Spring中的Bean函数

  1. /*
  2. * $filename: BeanUtils.java,v $
  3. * $Date: 2013-12-9  $
  4. * Copyright (C) ZhengHaibo, Inc. All rights reserved.
  5. * This software is Made by Zhenghaibo.
  6. */
  7. package edu.njupt.zhb.tools;
  8. import org.apache.struts2.ServletActionContext;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.support.FileSystemXmlApplicationContext;
  11. /*
  12. *@author: ZhengHaibo
  13. *web:     http://blog.csdn.net/nuptboyzhb
  14. *mail:    zhb931706659@126.com
  15. *2013-12-9  Nanjing,njupt,China
  16. */
  17. public class BeanUtils {
  18. /**
  19. * 获取Spring中注入的Bean
  20. * @param beanId:id
  21. * @return
  22. */
  23. public static Object getSpringBean(String beanId){
  24. //Spring配置文件的路径
  25. String xmlRealPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/applicationContext.xml");
  26. ApplicationContext ac = new FileSystemXmlApplicationContext(xmlRealPath);
  27. return ac.getBean(beanId);
  28. }
  29. }

2.5我们可以通过如下方式,获得Spring注入的值

  1. Student stu = (Student)BeanUtils.getSpringBean("student");

2.6用JSONObject工具打印一下stu对象的值

  1. System.out.println(JSONObject.fromObject(stu).toString());

2.7运行结果为:

  1. {"course":"Java","id":"1012010638","name":"Haibo","remarks":"Come from Properties","score":90}
未经允许不得用于商业目的

SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean的更多相关文章

  1. Spring读取配置文件,获取bean的几种方式

    BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...

  2. spring读取配置文件内容并自动注入

    添加注解: @PropertySource(value={"classpath:venus.properties"}) 示例: import org.springframework ...

  3. java web路径和spring读取配置文件

    此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...

  4. Spring 读取配置文件(一)

    注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...

  5. Spring 读取配置文件(二)

    Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...

  6. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

  7. SSH框架系列:Spring配置多个数据源

    分类: [java]2013-12-09 16:59 1247人阅读 评论(0) 收藏 举报 1.问题的引入 对于普通的SSH框架而言,一般配置一个数据源,一个SessionFactory,一个事务管 ...

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

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

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

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...

随机推荐

  1. 吴裕雄--天生自然Numpy库学习笔记:NumPy 线性代数

    import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) p ...

  2. Codeforces Round #620 (Div. 2) A. Two Rabbits

    Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...

  3. Python笔记⑤爬虫

    爬虫的前奏 # 爬虫前奏 # 明确目的 # 找到数据对应的网页 # 分析网页的结果找到数据所在的标签位置 # 模拟HTTP请求,向服务器发送这个请求,获取到服务器返回给我们的HTML # 用正则表达式 ...

  4. stack的使用-Hdu 1062

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. js加入收藏

    <script> function AddFavorite(sURL, sTitle) { try { window.external.addFavorite(sURL, sTitle); ...

  6. 2019年5月6日A股两百点暴跌行情思考

    原因:特朗普推特发布贸易战消息 盘面:跳空低开,单边下跌,上证指数最大跌幅200点,收盘千股跌停 操作:开盘加仓,盘中加仓,尾盘满仓 总结: 特大黑天鹅事件爆发引发大盘暴跌时,后续必将迎来一个反弹机会 ...

  7. .NET精彩博文

    vs2019安装和使用教程(详细) 为什么选择.NETCore? C#中Socket的简单使用

  8. C++的注册和回调

    注册回调的作用 在设计模式中注册回调的方式叫做回调模式.在SDK开发中,为增强开发者的SDK通用性,排序或者一些算法逻辑需要使用者进行编写.这时候就需要向SDK传递回调函数.注册回调能使下层主动与上层 ...

  9. 手搓SSM

    相关资料,网上的资料很多,但是文章看不懂,看别人写好的代码比较好理解 ssm-example mysssm 整个流程和原理 一个入口类,入口类需要在tomcat启动的时候执行 通过扫描文件加把文件取出 ...

  10. Interlocked.Increment()函数详解 (转载)

    原文地址 class Program { static object lockObj = new object(); ; ; //假设要处理的数据源 , ).ToList(); static void ...