SSH series: Spring reads the configuration file and getting Spring into Bean

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1 Introduction

Under the framework of SSH, if we take the configuration file in the src/ path of the project, the Spring configuration file is src/applicationContext.xml path, then we can use the Spring property-placeholder to read configuration files, and then injected into Bean. We in the program, according to Bean Id, get into thevalue. So we can use Spring to read the configuration file.


package edu.njupt.zhb.model.mysql;

 * Student entity. @author MyEclipse Persistence Tools

public class Student  implements {

    // 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) { = name;
        this.course = course;
        this.score = score;
    /** full constructor */
    public Student(String name, String course, Integer score, String remarks) { = name;
        this.course = course;
        this.score = score;
        this.remarks = remarks;

    // Property accessors

    public String getId() {
    public void setId(String id) { = id;

    public String getName() {
    public void setName(String 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;

The configuration

#student config
student.remarks=Come from Properties

The 2.3Spring configuration file applicationContext.xml configuration

<!Introduction of the datasource configuration file: -->
	<context:property-placeholder location="" />
	<bean id="student" class="edu.njupt.zhb.model.mysql.Student">
	    <property name="id" value="${}" />
        <property name="name" value="${}" />
        <property name="course" value="${student.course}" />
        <property name="score" value="${student.score}" />
        <property name="remarks" value="${student.remarks}" />

2.4 read Bean function in Spring

 * $filename:,v $
 * $Date: 2013-12-9  $
 * Copyright (C) ZhengHaibo, Inc. All rights reserved.
 * This software is Made by Zhenghaibo.

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;

 *@author: ZhengHaibo  
 *2013-12-9  Nanjing,njupt,China
public class BeanUtils {
	 * Gets the Spring implanted Bean
	 * @param beanId: id
	 * @return
	public static Object getSpringBean(String beanId){
		//The path to the Spring configuration file
		String xmlRealPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/applicationContext.xml");
		ApplicationContext ac = new FileSystemXmlApplicationContext(xmlRealPath);
		return ac.getBean(beanId);

2.5 we can through the following ways, Spring injectionvalue

Student stu = (Student)BeanUtils.getSpringBean("student");

2.6 using JSONObject tools to print the stu objectvalue


2.7 the results of:

{"course":"Java","id":"1012010638","name":"Haibo","remarks":"Come from Properties","score":90}

Without permission, shall not be used for commercial purposes

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jonathan at December 10, 2013 - 5:40 PM