SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean
1.简介
在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路径下,Spring的配置文件也是src/applicationContext.xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后注入Bean中。我们在程序中,可以根据Bean的Id,获取注入的值。这样我们就可以借助Spring来读取配置文件。
2.Code
2.1Student.java
- package edu.njupt.zhb.model.mysql;
- /**
- * Student entity. @author MyEclipse Persistence Tools
- */
- public class Student implements java.io.Serializable {
- // Fields
- private String id;
- private String name;
- private String course;
- private Integer score;
- private String remarks;
- // Constructors
- /** default constructor */
- public Student() {
- }
- /** minimal constructor */
- public Student(String name, String course, Integer score) {
- this.name = name;
- this.course = course;
- this.score = score;
- }
- /** full constructor */
- public Student(String name, String course, Integer score, String remarks) {
- this.name = name;
- this.course = course;
- this.score = score;
- this.remarks = remarks;
- }
- // Property accessors
- public String getId() {
- return this.id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getCourse() {
- return this.course;
- }
- public void setCourse(String course) {
- this.course = course;
- }
- public Integer getScore() {
- return this.score;
- }
- public void setScore(Integer score) {
- this.score = score;
- }
- public String getRemarks() {
- return this.remarks;
- }
- public void setRemarks(String remarks) {
- this.remarks = remarks;
- }
- }
2.2datasource.properties中的配置
- #student config
- student.name=Haibo
- student.id=1012010638
- student.course=Java
- student.score=90
- student.remarks=Come from Properties
2.3Spring配置文件applicationContext.xml部分配置
- <!-- 引入datasource配置文件 -->
- <context:property-placeholder location="classpath:datasource.properties" />
- <bean id="student" class="edu.njupt.zhb.model.mysql.Student">
- <property name="id" value="${student.id}" />
- <property name="name" value="${student.name}" />
- <property name="course" value="${student.course}" />
- <property name="score" value="${student.score}" />
- <property name="remarks" value="${student.remarks}" />
- </bean>
2.4读取Spring中的Bean函数
- /*
- * $filename: BeanUtils.java,v $
- * $Date: 2013-12-9 $
- * Copyright (C) ZhengHaibo, Inc. All rights reserved.
- * This software is Made by Zhenghaibo.
- */
- package edu.njupt.zhb.tools;
- import org.apache.struts2.ServletActionContext;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- /*
- *@author: ZhengHaibo
- *web: http://blog.csdn.net/nuptboyzhb
- *mail: zhb931706659@126.com
- *2013-12-9 Nanjing,njupt,China
- */
- public class BeanUtils {
- /**
- * 获取Spring中注入的Bean
- * @param beanId:id
- * @return
- */
- public static Object getSpringBean(String beanId){
- //Spring配置文件的路径
- String xmlRealPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/applicationContext.xml");
- ApplicationContext ac = new FileSystemXmlApplicationContext(xmlRealPath);
- return ac.getBean(beanId);
- }
- }
2.5我们可以通过如下方式,获得Spring注入的值
- Student stu = (Student)BeanUtils.getSpringBean("student");
2.6用JSONObject工具打印一下stu对象的值
- System.out.println(JSONObject.fromObject(stu).toString());
2.7运行结果为:
- {"course":"Java","id":"1012010638","name":"Haibo","remarks":"Come from Properties","score":90}
SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean的更多相关文章
- Spring读取配置文件,获取bean的几种方式
BeanFactory有很多实现类,通常使用 org.springframework.beans.factory.xml.XmlBeanFactory类.但对于大部分J2EE应用而言,推荐使 用App ...
- spring读取配置文件内容并自动注入
添加注解: @PropertySource(value={"classpath:venus.properties"}) 示例: import org.springframework ...
- java web路径和spring读取配置文件
此篇博客缘起:部署java web系统到阿里云服务器(ubuntu14.04)的时候,有以下两个问题 找不到自定义的property配置文件 上传图片的时候找不到路径 开发的时候是在windows上的 ...
- Spring 读取配置文件(一)
注册 @Configuration 标识的类,spring 读取配置文件的时候该类会被自动装载 package cn.com.receive;import org.springframework.be ...
- Spring 读取配置文件(二)
Spring 读取配置文件并调用 bean package cn.com.test.receive; import org.springframework.beans.factory.annotati ...
- 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境
前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...
- SSH框架系列:Spring配置多个数据源
分类: [java]2013-12-09 16:59 1247人阅读 评论(0) 收藏 举报 1.问题的引入 对于普通的SSH框架而言,一般配置一个数据源,一个SessionFactory,一个事务管 ...
- 关于spring读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
- Spring读取配置文件的几种方式
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; imp ...
随机推荐
- JS打开浏览器新窗口
window.open(URL,name,features,replace); 参数 描述 URL 一个可选的字符串,声明了要在新窗口中显示的文档的 URL.如果省略了这个参数,或者它的值是空字符串, ...
- C++中的sort函数和⾃定义cmp函数
写在最前面,本文摘录于柳神笔记: sort 函数在头⽂件 #include ⾥⾯,主要是对⼀个数组进⾏排序( int arr[] 数组或 者 vector 数组都⾏), vector 是容器,要⽤ v ...
- 深度学习之父低调开源 CapsNet,欲取代 CNN
“卷积神经网络(CNN)的时代已经过去了!” ——Geoffrey Hinton 酝酿许久,深度学习之父Geoffrey Hinton在10月份发表了备受瞩目的Capsule Networks(Cap ...
- Python语言——列表生成式
生成[1x1, 2x2, 3x3, ..., 10x10]列表: >>> L = [] >>> for x in range(1, 11):... >> ...
- 基于Facebook开源框架SocketRocket的即时通讯
SocketRocket 介绍: SocketRock 是 Facebook 开源的框架,基于 WebSocket 客户端类库,适用于 iOS.Mac OS.tv OS.GitHub 传送门:http ...
- 导入jeesite 项目
1:从开源中国用git方式下载jeesite源码 链接https://gitee.com/thinkgem/jeesite gti 地址:https://gitee.com/thinkgem/j ...
- Hibernate面试题(七)--load与get区别
1. load方式是延迟加载,只有属性被访问的时候才会调用sql语句 get方式是非延迟加载,无论后面的代码是否会访问到属性,马上执行sql语句 2. 都通过id=500去获取对象1. get方式会返 ...
- 【C++初学者自学笔记三】哑元函数、缺省参数、内联函数(模块二,PS:需要用到重载函数)
一,哑元函数:一个函数的参数只有类型没有名字的则这个参数称之为哑元.类似于void fun(int); 功能:1保持向前的兼容性,比方说我们需要做成一个成品,然后成品是会不断的更新第一代第二代,当我们 ...
- 工具,Linux - tree命令,显示程序树型结构
sudo apt-get install tree tree --help
- ava StringTokenizer 类使用方法
菜鸟教程 -- 学的不仅是技术,更是梦想! 首页 笔记首页 Android 互联网 程序员人生 程序员笑话 编程技术 红包 知识店铺 --> 首页 Android 鸡汤 逗乐 Search Ja ...