SSH Frame series: Spring reads the configuration file and gets the bean injected by spring

Source: Internet
Author: User

1. Introduction

Under the SSH framework, suppose we put the configuration file under the Src/datasource.properties path of the project, and the spring configuration file is also src/ Applicationcontext.xml path, then we can read the configuration file with spring's property-placeholder and inject it into the bean. In the program, we can get the injected value based on the Bean's ID. This allows us to read the configuration file by using 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 stude
        NT (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;
    The public void SetScore (Integer score) {This.score = score;
    Public String Getremarks () {return this.remarks;
    } public void Setremarks (String remarks) {this.remarks = remarks; }
}

Configuration in the 2.2datasource.properties

#student config
student.name=haibo
student.id=1012010638
student.course=java
student.score=90
Student.remarks=come from Properties

2.3Spring configuration file Applicationcontext.xml partial configuration

<!--introduce datasource profile-->
	<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 Reading the Bean function in spring

 * * $filename: beanutils.java,v $
 * $Date: 2013-12-9  $
 * Copyright (C) Zhenghaibo, Inc. All rights reserved.
 * This software was 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 {
	/**
	 * Get the bean injected in spring *
	 @param beanid:id
	 * @return
	/public static Object Getspringbean ( String Beanid) {
		//spring configuration file path
		string xmlrealpath = Servletactioncontext.getservletcontext (). Getrealpath ("/web-inf/classes/applicationcontext.xml");
		ApplicationContext ac = new Filesystemxmlapplicationcontext (xmlrealpath);
		Return Ac.getbean (Beanid);
	}


2.5 We can get the value of spring injection in the following way

Student stu = (Student) beanutils.getspringbean ("Student");

2.6 Print the value of the Stu object with the Jsonobject tool

System.out.println (Jsonobject.fromobject (Stu). toString ());

2.7 Running results are:

{"Course": "Java", "id": "1012010638", "name": "Haibo", "Remarks": "Come from Properties", "Score": 90}

Not allowed for commercial purposes without permission


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.