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

简介:     分类: 【java】2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路径下,Spring的配置文件也是src/applicationContext.xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后注入Bean中。
 

 

分类: 【java】

1.简介

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

2.Code

2.1Student.java

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package edu.njupt.zhb.model.mysql;  
  2.   
  3.   
  4.   
  5. /** 
  6.  * Student entity. @author MyEclipse Persistence Tools 
  7.  */  
  8.   
  9. public class Student  implements java.io.Serializable {  
  10.   
  11.   
  12.     // Fields      
  13.   
  14.      private String id;  
  15.      private String name;  
  16.      private String course;  
  17.      private Integer score;  
  18.      private String remarks;  
  19.   
  20.   
  21.     // Constructors  
  22.   
  23.     /** default constructor */  
  24.     public Student() {  
  25.     }  
  26.   
  27.     /** minimal constructor */  
  28.     public Student(String name, String course, Integer score) {  
  29.         this.name = name;  
  30.         this.course = course;  
  31.         this.score = score;  
  32.     }  
  33.       
  34.     /** full constructor */  
  35.     public Student(String name, String course, Integer score, String remarks) {  
  36.         this.name = name;  
  37.         this.course = course;  
  38.         this.score = score;  
  39.         this.remarks = remarks;  
  40.     }  
  41.   
  42.      
  43.     // Property accessors  
  44.   
  45.     public String getId() {  
  46.         return this.id;  
  47.     }  
  48.       
  49.     public void setId(String id) {  
  50.         this.id = id;  
  51.     }  
  52.   
  53.     public String getName() {  
  54.         return this.name;  
  55.     }  
  56.       
  57.     public void setName(String name) {  
  58.         this.name = name;  
  59.     }  
  60.   
  61.     public String getCourse() {  
  62.         return this.course;  
  63.     }  
  64.       
  65.     public void setCourse(String course) {  
  66.         this.course = course;  
  67.     }  
  68.   
  69.     public Integer getScore() {  
  70.         return this.score;  
  71.     }  
  72.       
  73.     public void setScore(Integer score) {  
  74.         this.score = score;  
  75.     }  
  76.   
  77.     public String getRemarks() {  
  78.         return this.remarks;  
  79.     }  
  80.       
  81.     public void setRemarks(String remarks) {  
  82.         this.remarks = remarks;  
  83.     }  
  84. }  

2.2datasource.properties中的配置

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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部分配置

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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函数

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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.   
  9. import org.apache.struts2.ServletActionContext;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  12.   
  13. /* 
  14.  *@author: ZhengHaibo   
  15.  *web:     http://blog.csdn.net/nuptboyzhb 
  16.  *mail:    zhb931706659@126.com 
  17.  *2013-12-9  Nanjing,njupt,China 
  18.  */  
  19. public class BeanUtils {  
  20.     /** 
  21.      * 获取Spring中注入的Bean 
  22.      * @param beanId:id 
  23.      * @return 
  24.      */  
  25.     public static Object getSpringBean(String beanId){  
  26.         //Spring配置文件的路径  
  27.         String xmlRealPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/applicationContext.xml");  
  28.         ApplicationContext ac = new FileSystemXmlApplicationContext(xmlRealPath);  
  29.         return ac.getBean(beanId);  
  30.     }  
  31. }  

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

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. Student stu = (Student)BeanUtils.getSpringBean("student");  

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

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. System.out.println(JSONObject.fromObject(stu).toString());  

2.7运行结果为:

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. {"course":"Java","id":"1012010638","name":"Haibo","remarks":"Come from Properties","score":90}  

 

未经允许不得用于商业目的
目录
相关文章
|
9天前
|
存储 安全 Java
事件的力量:探索Spring框架中的事件处理机制
事件的力量:探索Spring框架中的事件处理机制
26 0
|
18天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
30 1
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
11 0
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
ssh(Spring+Spring mvc+hibernate)——BaseDaoImpl.java
12 0
|
1月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
45 0
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——Dept.java
ssh(Spring+Spring mvc+hibernate)——Dept.java
12 0
|
7天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
14 1
|
7天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
17 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
17天前
|
开发框架 安全 Java
探索 Spring 框架:企业级应用开发的强大工具
探索 Spring 框架:企业级应用开发的强大工具
18 1
|
30天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
ssh(Spring+Spring mvc+hibernate)——showDept.jsp
8 0