现在的位置: 首页 > 综合 > 正文

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

2017年12月10日 ⁄ 综合 ⁄ 共 2929字 ⁄ 字号 评论关闭

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}

未经允许不得用于商业目的


抱歉!评论已关闭.