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

Source: Internet
Author: User

 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
  1. Package edu.njupt.zhb.model.mysql;
  2. /**
  3. * Student entity. @author MyEclipse Persistence Tools
  4. */
  5. Public class Student implements Java.io.Serializable {
  6. // fields
  7. private String ID;
  8. private String name;
  9. Private String course;
  10. private Integer score;
  11. private String remarks;
  12. //Constructors
  13. /** Default constructor * /
  14. Public Student () {
  15. }
  16. /** Minimal Constructor * *
  17. Public Student (string name, string course, Integer score) {
  18. this.name = name;
  19. this.course = course;
  20. This.score = score;
  21. }
  22. /** Full constructor * /
  23. Public Student (string name, string course, Integer Score, String remarks) {
  24. this.name = name;
  25. this.course = course;
  26. This.score = score;
  27. This.remarks = remarks;
  28. }
  29. //Property accessors
  30. Public String getId () {
  31. return this.id;
  32. }
  33. public void SetId (String id) {
  34. this.id = ID;
  35. }
  36. Public String GetName () {
  37. return this.name;
  38. }
  39. public void SetName (String name) {
  40. this.name = name;
  41. }
  42. Public String GetCourse () {
  43. return This.course;
  44. }
  45. public void Setcourse (String course) {
  46. this.course = course;
  47. }
  48. Public Integer Getscore () {
  49. return This.score;
  50. }
  51. public void SetScore (Integer score) {
  52. This.score = score;
  53. }
  54. Public String Getremarks () {
  55. return This.remarks;
  56. }
  57. public void Setremarks (String remarks) {
  58. This.remarks = remarks;
  59. }
  60. }

Configuration in the 2.2datasource.properties

[Plain]View Plaincopy
    1. #student Config
    2. Student.name=haibo
    3. student.id=1012010638
    4. Student.course=java
    5. Student.score=90
    6. Student.remarks=come from Properties

2.3Spring configuration file Applicationcontext.xml partial configuration

[HTML]View Plaincopy
  1. <!--Introducing DataSource Profiles--
  2. <context:property-placeholder location="classpath:datasource.properties" />
  3. <Bean id= "student" class="edu.njupt.zhb.model.mysql.Student">
  4. <property name="id" value="${student.id}" />
  5. <property name="name" value="${student.name}" />
  6. <property name="course" value="${student.course}" />
  7. <property name="score" value="${student.score}" />
  8. <property name="Remarks" value="${student.remarks}" />
  9. </Bean>

2.4 Reading the Bean function in spring

[Java]View Plaincopy
  1. /*
  2. * $filename: Beanutils.java,v $
  3. * $Date: 2013-12-9 $
  4. * Copyright (C) Zhenghaibo, Inc. All rights reserved.
  5. * This software are made by Zhenghaibo.
  6. */
  7. Package edu.njupt.zhb.tools;
  8. Import Org.apache.struts2.ServletActionContext;
  9. Import Org.springframework.context.ApplicationContext;
  10. Import Org.springframework.context.support.FileSystemXmlApplicationContext;
  11. /*
  12. * @author: Zhenghaibo
  13. *web:http://blog.csdn.net/nuptboyzhb
  14. *mail: [Email protected]
  15. *2013-12-9 Nanjing,njupt,china
  16. */
  17. Public class Beanutils {
  18. /** 
  19. * Get the beans injected in spring
  20. * @param beanid:id
  21. * @return
  22. */
  23. public static Object Getspringbean (String beanid) {
  24. path to the//spring configuration file
  25. String Xmlrealpath = Servletactioncontext.getservletcontext (). Getrealpath ("/web-inf/classes/  Applicationcontext.xml ");
  26. ApplicationContext AC = new Filesystemxmlapplicationcontext (Xmlrealpath);
  27. return Ac.getbean (Beanid);
  28. }
  29. }

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

[Java]View Plaincopy
    1. Student stu = (Student) beanutils.getspringbean ("Student");

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

[Java]View Plaincopy
    1. System.out.println (Jsonobject.fromobject (Stu). toString ());

2.7 The result of the operation is:

[Plain]View Plaincopy
    1. {"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

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.