zoukankan      html  css  js  c++  java
  • SSH框架系列:Spring读取配置文件以及获取Spring注入的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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在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 plaincopy在CODE上查看代码片派生到我的代码片
     
    1. Student stu = (Student)BeanUtils.getSpringBean("student");  

    2.6用JSONObject工具打印一下stu对象的值
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. System.out.println(JSONObject.fromObject(stu).toString());  

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

    未经允许不得用于商业目的
  • 相关阅读:
    raw_input() 与 input()对比
    你很熟悉CSS,却没掌握这些CSS技巧
    CSS样式设置
    javascript基本语法和变量(转)
    手机/移动前端开发需要注意的20个要点
    移动端”宴席知多少
    git第一次提交代码到远程仓库
    java对过反射调用方法
    站点收集
    别人抢红包,我们研究一下红包算法
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4168488.html
Copyright © 2011-2022 走看看