需要加载的配置文件内容(resource.properties):

  1. #FTP相关配置
  2. #FTP的IP地址
  3. FTP_ADDRESS=192.168.1.121
  4. FTP_PORT=21
  5. FTP_USERNAME=ftpuser
  6. FTP_PASSWORD=root
  7. FTP_BASE_PATH=/home/ftpuser/taotao/images
  8.  
  9. #图片服务器的相关配置
  10. #图片服务器的基础URL
  11. IMAGE_BASE_URL=http://192.168.1.121/images

实现在applicationContext.xml中需要配置加载的配置文件:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  11. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  12.  
  13. <!-- 加载配置文件 -->
  14. <context:property-placeholder location="classpath:resource/*.properties" />
  15.  
  16. </beans>

然后使用注解方式获取配置文件中的信息:

  1. package com.taotao.service.impl;
  2. import com.taotao.service.PictureService;
  3.  
  4. @Service
  5. public class PictureServiceImpl implements PictureService
  6. {
  7. @Value("${FTP_ADDRESS}")
  8. private String FTP_ADDRESS;
  9.  
  10. @Value("${FTP_PORT}")
  11. private Integer FTP_PORT;
  12.  
  13. @Value("${FTP_USERNAME}")
  14. private String FTP_USERNAME;
  15.  
  16. @Value("${FTP_PASSWORD}")
  17. private String FTP_PASSWORD;
  18.  
  19. @Value("${FTP_BASE_PATH}")
  20. private String FTP_BASE_PATH;
  21.  
  22. @Value("${IMAGE_BASE_URL}")
  23. private String IMAGE_BASE_URL;
  24.  
  25. }

这样既可以使用配置文件中的字段了。

使用Spring注解获取配置文件信息的更多相关文章

  1. SpringBoot项目中,获取配置文件信息

    1.在配置文件中设置信息,格式如下 wechat: mpAppId: wxdf2b09f280e6e6e2 mpAppSecret: f924b2e9f140ac98f9cb5317a8951c71 ...

  2. Spring Boot使用@Value注解获取配置文件中的属性

    获取配置文件的内容——

  3. SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式

    Spring入门篇:https://www.cnblogs.com/biehongli/p/10170241.html SpringBoot的默认的配置文件application.properties ...

  4. 获取配置文件信息——configparser

    配置文件host.int格式如下: [host]product=xxxxxxxxxxtest=xxxxxxxxxx python 3.x代码如下: import os,configparser def ...

  5. .Net FrameWork获取配置文件信息

    今天在做项目的时候需要到配置文件中读取信息,之前自己并没有操作过,今天算是完成自己的一个心愿.读取配置文件中的信息并不难,大致思路是: 1.载入配置文件,获取配置文件的实例. 2.获取配置文件的节点 ...

  6. mybatis整合spring获取配置文件信息出错

    描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...

  7. Spring MVC通过Pageable对象和PageableDefault注解获取分页信息(MongoDB通过Pageable来操作分页)

    说明:Pageable同时也能用于操作MongoDB的分页. PageableSpring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息( ...

  8. Spring Boot使用@ConfigurationProperties注解获取配置文件中的属性值

    注意:这种方式要提供属性的getter/setter方法—— 如果idea报错,提示没有相应的执行器,就需要在maven中添加: (虽然不配置代码也能正常运行,作用在下面会说明) 配置了该执行器后,在 ...

  9. Sping boot 之 @Value("${xxx}") 注解获取配置文件内容

    1.注解方式读取 1-1.@PropertySource配置文件路径设置,在类上添加注解,如果在默认路径下可以不添加该注解. 需要用@PropertySource的有: 例如非application. ...

随机推荐

  1. jquery -- body div 和 body>div 的区别

    body  div表示body下的所有div标签都应用样式,不管是body的儿子还是孙子或更孙子 body >div表示只有body的儿子div才应用样式,孙子以后都不应用

  2. Worktile中百万级实时消息推送服务的实现

    Worktile中百万级实时消息推送服务的实现 出自:http://blog.jobbole.com/81125/

  3. c#用run32dll打开系统dll(如系统图片查看器,并置最顶层)

    [DllImport("user32.dll", EntryPoint = "SetWindowPos",CharSet = CharSet.Auto)] st ...

  4. JavaScript 事件循环 — event loop

    引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...

  5. windows下查看dns缓存和刷新缓存

    ipconfig /displaydns 显示已有缓存,可能比较大建议 ipconfig /displaydns > C:\Users\SDWQ\Desktop\1.txt 再查看. 强制更新缓 ...

  6. Object Slicing in C++

    In C++, a derived class object can be assigned to base class, but the other way is not possible. cla ...

  7. Jquery-easyUi------(布局)

    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> <! ...

  8. Linux命令之乐--read

    read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 -p:后接屏幕输出的提示语句 -n:设定输入的字符个数,当达到指定的个数则自动退出,并将输入赋予给变量 -t:当输 ...

  9. Securi-Pi:使用树莓派作为安全跳板

    导读 像很多 LinuxJournal 的读者一样,我也过上了当今非常普遍的“科技游牧”生活,在网络之间,从一个接入点到另一个接入点,我们身处现实世界的不同地方却始终保持连接到互联网和日常使用的其它网 ...

  10. iOS-多线程的底层实现

    (1)首先回答什么是线程 1个进程要想执行任务,必须得有线程.线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 (2)什么是多线程 1个进程中可以开启多条线程,每条线程可以并行(同时 ...