SSH框架系列:Spring读取配置文件以及获取Spring注入的Bean

简介:     分类: 【java】2013-12-09 16:29 1020人阅读 评论(0) 收藏 举报 1.简介 在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路径下,Spring的配置文件也是src/applicationContext.xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后注入Bean中。
 

 

分类: 【java】

1.简介

在SSH框架下,假设我们将配置文件放在项目的src/datasource.properties路径下,Spring的配置文件也是src/applicationContext.xml路径下,那么我们可以借助Spring的property-placeholder读取配置文件,然后注入Bean中。我们在程序中,可以根据Bean的Id,获取注入的值。这样我们就可以借助Spring来读取配置文件。

2.Code

2.1Student.java

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package edu.njupt.zhb.model.mysql;  
  2.   
  3.   
  4.   
  5. /** 
  6.  * Student entity. @author MyEclipse Persistence Tools 
  7.  */  
  8.   
  9. public class Student  implements java.io.Serializable {  
  10.   
  11.   
  12.     // Fields      
  13.   
  14.      private String id;  
  15.      private String name;  
  16.      private String course;  
  17.      private Integer score;  
  18.      private String remarks;  
  19.   
  20.   
  21.     // Constructors  
  22.   
  23.     /** default constructor */  
  24.     public Student() {  
  25.     }  
  26.   
  27.     /** minimal constructor */  
  28.     public Student(String name, String course, Integer score) {  
  29.         this.name = name;  
  30.         this.course = course;  
  31.         this.score = score;  
  32.     }  
  33.       
  34.     /** full constructor */  
  35.     public Student(String name, String course, Integer score, String remarks) {  
  36.         this.name = name;  
  37.         this.course = course;  
  38.         this.score = score;  
  39.         this.remarks = remarks;  
  40.     }  
  41.   
  42.      
  43.     // Property accessors  
  44.   
  45.     public String getId() {  
  46.         return this.id;  
  47.     }  
  48.       
  49.     public void setId(String id) {  
  50.         this.id = id;  
  51.     }  
  52.   
  53.     public String getName() {  
  54.         return this.name;  
  55.     }  
  56.       
  57.     public void setName(String name) {  
  58.         this.name = name;  
  59.     }  
  60.   
  61.     public String getCourse() {  
  62.         return this.course;  
  63.     }  
  64.       
  65.     public void setCourse(String course) {  
  66.         this.course = course;  
  67.     }  
  68.   
  69.     public Integer getScore() {  
  70.         return this.score;  
  71.     }  
  72.       
  73.     public void setScore(Integer score) {  
  74.         this.score = score;  
  75.     }  
  76.   
  77.     public String getRemarks() {  
  78.         return this.remarks;  
  79.     }  
  80.       
  81.     public void setRemarks(String remarks) {  
  82.         this.remarks = remarks;  
  83.     }  
  84. }  

2.2datasource.properties中的配置

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  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配置文件applicationContext.xml部分配置

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <!-- 引入datasource配置文件 -->  
  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读取Spring中的Bean函数

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. /* 
  2.  * $filename: BeanUtils.java,v $ 
  3.  * $Date: 2013-12-9  $ 
  4.  * Copyright (C) ZhengHaibo, Inc. All rights reserved. 
  5.  * This software is Made by Zhenghaibo. 
  6.  */  
  7. package edu.njupt.zhb.tools;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  12.   
  13. /* 
  14.  *@author: ZhengHaibo   
  15.  *web:     http://blog.csdn.net/nuptboyzhb 
  16.  *mail:    zhb931706659@126.com 
  17.  *2013-12-9  Nanjing,njupt,China 
  18.  */  
  19. public class BeanUtils {  
  20.     /** 
  21.      * 获取Spring中注入的Bean 
  22.      * @param beanId:id 
  23.      * @return 
  24.      */  
  25.     public static Object getSpringBean(String beanId){  
  26.         //Spring配置文件的路径  
  27.         String xmlRealPath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/applicationContext.xml");  
  28.         ApplicationContext ac = new FileSystemXmlApplicationContext(xmlRealPath);  
  29.         return ac.getBean(beanId);  
  30.     }  
  31. }  

2.5我们可以通过如下方式,获得Spring注入的值

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. Student stu = (Student)BeanUtils.getSpringBean("student");  

2.6用JSONObject工具打印一下stu对象的值

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. System.out.println(JSONObject.fromObject(stu).toString());  

2.7运行结果为:

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. {"course":"Java","id":"1012010638","name":"Haibo","remarks":"Come from Properties","score":90}  

 

未经允许不得用于商业目的
目录
相关文章
|
8天前
|
开发框架 运维 监控
Spring Boot中的日志框架选择
在Spring Boot开发中,日志管理至关重要。常见的日志框架有Logback、Log4j2、Java Util Logging和Slf4j。选择合适的日志框架需考虑性能、灵活性、社区支持及集成配置。本文以Logback为例,演示了如何记录不同级别的日志消息,并强调合理配置日志框架对提升系统可靠性和开发效率的重要性。
|
30天前
|
设计模式 XML Java
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
|
26天前
|
Java 开发者 Spring
理解和解决Spring框架中的事务自调用问题
事务自调用问题是由于 Spring AOP 代理机制引起的,当方法在同一个类内部自调用时,事务注解将失效。通过使用代理对象调用、将事务逻辑分离到不同类中或使用 AspectJ 模式,可以有效解决这一问题。理解和解决这一问题,对于保证 Spring 应用中的事务管理正确性至关重要。掌握这些技巧,可以提高开发效率和代码的健壮性。
86 13
|
29天前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
|
29天前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
|
1月前
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
67 6
|
1月前
|
IDE Java 测试技术
互联网应用主流框架整合之Spring Boot开发
通过本文的介绍,我们详细探讨了Spring Boot开发的核心概念和实践方法,包括项目结构、数据访问层、服务层、控制层、配置管理、单元测试以及部署与运行。Spring Boot通过简化配置和强大的生态系统,使得互联网应用的开发更加高效和可靠。希望本文能够帮助开发者快速掌握Spring Boot,并在实际项目中灵活应用。
56 5
|
1月前
|
XML Java 数据格式
🌱 深入Spring的心脏:Bean配置的艺术与实践 🌟
本文深入探讨了Spring框架中Bean配置的奥秘,从基本概念到XML配置文件的使用,再到静态工厂方式实例化Bean的详细步骤,通过实际代码示例帮助读者更好地理解和应用Spring的Bean配置。希望对你的Spring开发之旅有所助益。
118 3