Category: "java"2013-12-09 16:29 1020 People read reviews (0) favorite reports
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, we can read the configuration file with spring Property-placeholder and inject it into the bean. In our program, we can get the injected value based on the Bean's ID. This allows us to read the configuration file with spring.
2.Code
2.1student.java
[Java]View Plaincopy
- 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;
- }
- }
Configuration in the 2.2datasource.properties
[Plain]View Plaincopy
- #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
[HTML]View Plaincopy
- <!--Introducing DataSource Profiles--
- <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
[Java]View Plaincopy
- /*
- * $filename: Beanutils.java,v $
- * $Date: 2013-12-9 $
- * Copyright (C) Zhenghaibo, Inc. All rights reserved.
- * This software are 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: [Email protected]
- *2013-12-9 Nanjing,njupt,china
- */
- Public class Beanutils {
- /**
- * Get the beans injected in spring
- * @param beanid:id
- * @return
- */
- public static Object Getspringbean (String beanid) {
- 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 get the value of spring injection in the following way
[Java]View Plaincopy
- Student stu = (Student) beanutils.getspringbean ("Student");
2.6 Print the value of the Stu object using the Jsonobject tool
[Java]View Plaincopy
- System.out.println (Jsonobject.fromobject (Stu). toString ());
2.7 The result of the operation is:
[Plain]View Plaincopy
- {"Course": "Java", "id": "1012010638", "name": "Haibo", "Remarks": "Come from Properties", "Score": 90}
Not permitted for commercial purposes without permission
SSH Framework Series: Spring reads the configuration file and gets the bean injected by spring