`

spring 获取配置文件properties中参数

 
阅读更多

spring 获取配置文件properties中参数

 
<iframe id="cproIframe_u728416_1" style="border-width: 0px; margin: 0px; padding: 0px;" src="http://pos.baidu.com/acom?adn=3&amp;at=134&amp;aurl=&amp;cad=1&amp;ccd=24&amp;cec=UTF-8&amp;cfv=16&amp;ch=0&amp;col=zh-CN&amp;conOP=0&amp;cpa=1&amp;dai=1&amp;dis=0&amp;layout_filter=rank%2Ctabcloud&amp;ltr=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D8%26rsv_bp%3D1%26tn%3Dbaidu%26wd%3Dspring%2520mvc%2520PropertyPlaceholderConfigurer%26rsv_pq%3D857a1d3400010732%26rsv_t%3D3e3dkpSNLQpYm%252Bzw8TbjldufYNimprz532c5sHfxK7e9mMRZaeb7FK0wiYA%26bs%3Dspring%2520mvc%2520propertisutil&amp;ltu=http%3A%2F%2Fblog.94gleaner.com%2F445.html&amp;lunum=6&amp;n=74015068_cpr&amp;pcs=1366x665&amp;pis=10000x10000&amp;ps=169x878&amp;psr=1366x768&amp;pss=1366x170&amp;qn=2af92e2cbaa81964&amp;rad=&amp;rsi0=336&amp;rsi1=280&amp;rsi5=4&amp;rss0=%23FFFFFF&amp;rss1=%23FFFFFF&amp;rss2=%230000FF&amp;rss3=%23444444&amp;rss4=%23008000&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=&amp;td_id=728416&amp;tn=text_default_336_280&amp;tpr=1423746810626&amp;ts=1&amp;version=2.0&amp;xuanting=0&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u728416&amp;tt=1423746809723.906.2326.2340" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center,center" width="336" height="280"></iframe>

Spring中config属性文件的读取与使用 PropertyPlaceholderConfigurer 注解@Component

1.配置文件:/WEB-INF/configInfo.properties
配置文件内容:
email.host = www.94gleaner.com
email.port = xxx
email.username = gleaner
email.password = xxx
email.sendFrom = 94gleaner@94gleaner.com

2.Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下:
<!– spring的属性加载器,加载properties文件中的属性 –>
<bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>

  1. <property name=”location”>
    1. <value>/WEB-INF/configInfo.properties</value>
  2. </property>
  3. <property name=”fileEncoding” value=”utf-8″ />

</bean>

3.其它bean中引用,例:
<property name=”host”>

  1. <value>${email.host}</value>

</property>
<property name=”port”>

  1. <value>${email.port}</value>

</property>

4.JAVA代码中获取方法:创建ConfigInfo.java
@Component(“configInfo”)
public class ConfigInfo {

  1. @Value(“${email.host}”)
  2. private String host;
  3. @Value(“${email.port}”)
  4. private String port;
  5. public String getHost() {
    1. return host;
  6. }
  7. public String getPort() {
    1. return port;
  8. }

}

5.代码中调用处
@Autowired
private ConfigInfo configInfo;
public byte[] Test(HttpServletResponse response) {

  1. string host = configInfo.getHost();

}

分享到:
评论

相关推荐

    带有外部定义集合命名空间的Spring配置文件

    该xml文件不仅仅是Spring框架初次使用时的主配置文件,而且该文件里面包含了java.util里面的List,Set,Map,Properties等的命名空间,支持在配置文件中定义外部的集合,便于参数注入的复用性

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...

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

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    基于Redis的分布式配置管理中心.zip

    即Redis和文件缓存中都找不到配置项时,客户端会从配置文件(app.properties)中加载。 在本地开发环境,可以不连接Redis,只使用配置文件(app.properties),以便于测试。 服务端 GUI尚未实现,暂时只能在key=config:{...

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

     由于Spring通过代理Hibernate完成数据层的操作,所以原Hibernate的配置文件hibernate.cfg.xml的信息也转移到Spring的配置文件中:  代码 4 Spring中有关Hibernate的配置信息 1. 2. !-- 数据源的配置 //--> 3. ...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    mina2.0+spring

    自己整理结合网上的诸多资料开发的一个中间件,当然这个是demo,想做成完整的通讯中间件的话需要在handler处做处理,并对加载配置文件处做修改方可导出成jar集成到其他系统。 这里简单说下: 1.ij.properties配置...

    Spring API

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring-boot-整合redis

    配置Redis连接:在application.properties或application.yml文件中配置Redis服务器的地址、端口以及其他相关参数,如密码、数据库索引等。 使用RedisTemplate:Spring Data Redis提供了一个名为RedisTemplate的类,...

    springmybatis

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

    mybatis-generator-spring-cloud-maven-plugin:这个Maven插件专门用于在由spring-cloud-config配置的数据源上为Mybatis生成代码。

    Mybatis已经有开发了一个“mybatis-generator-maven-plugin”插件供大众使用了,但该插件仅能通过配置固定的常量参数来使用数据源信息,无法满足从spring-cloud-config配置文件中读取数据源配置信息来生成代码。...

    基于Dubbo实现的SOA分布式(没有实现分布式事务)-SpringBoot整合各种组件的JavaWeb脚手架+源代码+文档

    - 在Controller中使用@PreAuthorize等注解需要在spring-web配置文件中扫描security包下的类 6. 引用application.properties中的属性的方式:@ConfigurationProperties(prefix = "spring.mail") + @Component + ...

    Struts2属性文件详解

    该属性指定加载Struts 2配置文件的配置文件管理器.该属性的默认值是org.apache.Struts2.config.DefaultConfiguration, 这是Struts 2默认的配置文件管理器.如果需要实现自己的配置管理器,开发者则可以实现一个实现...

    mybatis-file-generator:可扩展的mybatis数据库CRUD配置文件的一键生成工具

    并且期望有application.properties或application.yml文件之一,并且文件中需要有spring.application.name参数;import com.mylomen.mybatis.domain.XxqMybatisBO;import com.mylomen.mybatis.helper.DbInfoHelper;...

    springboot参考指南

    改变应用程序外部配置文件的位置 iii. 63.3. 使用'short'命令行参数 iv. 63.4. 使用YAML配置外部属性 v. 63.5. 设置生效的Spring profiles vi. 63.6. 根据环境改变配置 vii. 63.7. 发现外部属性的内置选项 iii. 64....

    基于ssm+Vue的ssm无纸化考试模拟系统(源码+部署说明+系统介绍+数据库).zip

    配置Spring和MyBatis:根据实际情况修改mybatis-config.xml和spring-mvc.xml文件中的配置信息,以便系统能够正确读取数据库表结构和执行SQL语句。启动项目:在命令行中进入项目根目录,执行./mvnw spring-boot:run...

    好用的代码生成源码

    配置简单,只有一个配置文件generator.properties 生成器的运行 在eclipse中运行 配置classpath,将generator/lib中的rapid-generator.jar及其它数据库驱动加入classpath 修改generator.xml的数据库连接属性及其它属性...

Global site tag (gtag.js) - Google Analytics