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

1 Introduction

Under the framework of SSH, if we take the configuration file in the src/datasource.properties 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.

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;
    }
}

The 2.2datasource.properties configuration

#student config
student.name=Haibo
student.id=1012010638
student.course=Java
student.score=90
student.remarks=Come from Properties

The 2.3Spring configuration file applicationContext.xml configuration

<!Introduction of the datasource configuration file: -->
	<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 read Bean function in Spring

/*
 * $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:      
 *mail:    
 *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

System.out.println(JSONObject.fromObject(stu).toString());

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


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