`

Spring初始化,将配置文件properties注入到bean中

阅读更多

首先是Spring的配置文件

applicationContext.xml 

<!-- 打开注解 -->
	<context:annotation-config/>
	<!-- 扫描包,一定要扫描到相应的VO类 -->
	<context:component-scan base-package="com.ssh.*" />
    <!-- 加载properties文件,用于往javabean中注入 -->
	<bean name="myproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    	<property name="locations">
    		<list>
    			<value>classpath:META-INF/app_config/properties/sysconfig.properties</value>
    			<value>classpath:META-INF/app_config/properties/sysconfig-debug.properties</value>
    		</list>
    	</property>
    </bean>
    <!-- 加载properties文件,用于替换Spring 配置文件中的占位符变量(${varible}) -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="properties" ref="myproperties"></property>
    	<property name="locations">
    		<list>
    			<value>classpath:META-INF/app_config/properties/global.framwork.properties</value>
    		</list>
    	</property>
    </bean>

 

SysConfigVo

package com.ssh.service;

import org.springframework.stereotype.Component;
import org.springframework.bean.factory.annotation.Value;

@Component
public class SysConfig {

	//myProperties必须和applicationContext.xml中的PropertiesFactoryBean的name一样
	@Value("#{myProperties}['gloable.model']")
	private String gloable_model;

	public String getGloable_model() {
		return gloable_model;
	}

	public void setGloable_model(String gloable_model) {
		this.gloable_model = gloable_model;
	}
	
}

 在service中使用该VO

package com.ssh.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.dao.UserDao;
import com.ssh.entity.User;

@Service
public class UserService {

	@Resource
	private SysConfig sysConfig;
	logger.info(sysConfig.getGloable_model());
	
}

 

 

分享到:
评论

相关推荐

    spring.doc

    Lazy-init初始化bean的时机拓展: 15 3.4 Bean的作用域 16 Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定...

    Spring-Reference_zh_CN(Spring中文参考手册)

    1. 简介 1.1. 概览 1.2. 使用场景 2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 ... 将 Spring Beans 注入到 Tapestry ...

    Spring中文帮助文档

    3.3.4. 延迟初始化bean 3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的...

    SpringBoot启动过程-mind版.md

    1. **加载配置:** Spring Boot会读取项目中的配置文件(如`application.properties`或`application.yml`),并将其中的配置信息加载到内存中,以供后续使用。 2. **初始化应用上下文:** Spring Boot会创建一个...

    吴天雄--Spring笔记.doc

    Spring框架简介(EJB、JMX、Spring核心功能、Spring模块详解、Spring重要概念(容器)、Spring容器初始化的整个流程、Spring后处理器),IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性详解(包含...

    springboot学习思维笔记.xmind

    Bean的初始化和销毁 Java配置方式 注解方式 Profile @Profile 通过设定jvm的spring.profiles.active参数 web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,...

    Spring API

    3.3.4. 延迟初始化bean 3.3.5. 自动装配(autowire)协作者 3.3.6. 依赖检查 3.3.7. 方法注入 3.4. Bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. Singleton beans和prototype-bean的...

    Spring.zip

    Spring注入基本值,Spring注入集合和数组,利用Spring初始化数据库连接池,Spring 表达式读取对象(Bean、数组、Map、Properties)的属性值,bean的注入方式等。

    spring chm文档

    3.3.5. 延迟初始化bean 3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 ...

    Spring 2.0 开发参考手册

    3.3.5. 延迟初始化bean 3.3.6. 自动装配(autowire)协作者 3.3.7. 依赖检查 3.3.8. 方法注入 3.4. bean的作用域 3.4.1. Singleton作用域 3.4.2. Prototype作用域 3.4.3. 其他作用域 3.4.4. 自定义作用域 ...

    ssh(structs,spring,hibernate)框架中的上传下载

     在配置完LobHandler后, 还需要将其注入到sessionFactory的Bean中,下面是调用后的sessionFactory Bean的配置:  代码 6 将lobHandler注入到sessionFactory中的配置 1. 2. … 3. <bean id="sessionFactory" 4. ...

    spring boot源码

    4. 容器初始化过程中追加各种功能,例如统计时间、输出日志等 监听器类型 1. 在应用运行但未进行任何处理时,将发送 ApplicationStartingEvent。 2. 当Environment被使用,且上下文创建之前,将发送 ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part3

    2.3 Bean 的初始化和销毁 37 2.3.1 点睛 37 2.3.2 演示 38 2.4 Profile 40 2.4.1 点睛 40 2.4.2 演示 41 2.5 事件(Application Event) 44 2.5.1 点睛 44 2.5.2 示例 44 第3 章 Spring 高级话题 48 3.1 Spring ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part2

    2.3 Bean 的初始化和销毁 37 2.3.1 点睛 37 2.3.2 演示 38 2.4 Profile 40 2.4.1 点睛 40 2.4.2 演示 41 2.5 事件(Application Event) 44 2.5.1 点睛 44 2.5.2 示例 44 第3 章 Spring 高级话题 48 3.1 Spring ...

    JavaEE开发的颠覆者SpringBoot实战[完整版].part1

    2.3 Bean 的初始化和销毁 37 2.3.1 点睛 37 2.3.2 演示 38 2.4 Profile 40 2.4.1 点睛 40 2.4.2 演示 41 2.5 事件(Application Event) 44 2.5.1 点睛 44 2.5.2 示例 44 第3 章 Spring 高级话题 48 3.1 Spring ...

    ssh2(struts2+spring2.5+hibernate3.3)自动生成模版

    {自定义的存放包}目录:4个xml文件(applicationContext-dao.xml(dao注入配置),applicationContext-service.xml(service注入配置),action-servlet.xml(action注入配置),struts-{自定义的存放包名}.xml(struts2的...

    springmybatis

    1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...

    springboot参考指南

    使用Spring JDBC初始化数据库 iv. 68.4. 初始化Spring Batch数据库 v. 68.5. 使用一个高级别的数据迁移工具 i. 68.5.1. 启动时执行Flyway数据库迁移 ii. 68.5.2. 启动时执行Liquibase数据库迁移 viii. 69. 批处理...

    mybatis-framework修改生成

    2.创建好数据库之后建立好自己的业务表同时执行脚本jeecgmybatis.sql,将系统自带的表初始化到新建的数据库中 3.使用myeclipse新建web工程之后将jeecg-mybatis-framework目录下文件复制粘贴到新建的web工程目录下 4....

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    BlazeDS 将读取 services-config.xml 配置文件,该配置文件又引用了 remoting-config.xml、proxy-config.xml 和 messaging-config.xml 这 3 个配置文件,所以,一共需要 4 个配置文件。 由于 BlazeDS 需要将 Java ...

Global site tag (gtag.js) - Google Analytics