首页 > 程序开发 > 软件开发 > Java >

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

2013-12-10

1 简介 在SSH框架下,假设我们将配置文件放在项目的src datasource properties路径下,Spring的配置文件也是src applicationContext xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后

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部分配置


	
	
	    
        
        
        
        
	

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}

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


相关文章
最新文章
热点推荐