使用Spring注解获取配置文件信息
需要加载的配置文件内容(resource.properties):
#FTP相关配置
#FTP的IP地址
FTP_ADDRESS=192.168.1.121
FTP_PORT=21
FTP_USERNAME=ftpuser
FTP_PASSWORD=root
FTP_BASE_PATH=/home/ftpuser/taotao/images #图片服务器的相关配置
#图片服务器的基础URL
IMAGE_BASE_URL=http://192.168.1.121/images
实现在applicationContext.xml中需要配置加载的配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties" /> </beans>
然后使用注解方式获取配置文件中的信息:
package com.taotao.service.impl;
import com.taotao.service.PictureService; @Service
public class PictureServiceImpl implements PictureService
{
@Value("${FTP_ADDRESS}")
private String FTP_ADDRESS; @Value("${FTP_PORT}")
private Integer FTP_PORT; @Value("${FTP_USERNAME}")
private String FTP_USERNAME; @Value("${FTP_PASSWORD}")
private String FTP_PASSWORD; @Value("${FTP_BASE_PATH}")
private String FTP_BASE_PATH; @Value("${IMAGE_BASE_URL}")
private String IMAGE_BASE_URL; }
这样既可以使用配置文件中的字段了。
使用Spring注解获取配置文件信息的更多相关文章
- SpringBoot项目中,获取配置文件信息
1.在配置文件中设置信息,格式如下 wechat: mpAppId: wxdf2b09f280e6e6e2 mpAppSecret: f924b2e9f140ac98f9cb5317a8951c71 ...
- Spring Boot使用@Value注解获取配置文件中的属性
获取配置文件的内容——
- SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式
Spring入门篇:https://www.cnblogs.com/biehongli/p/10170241.html SpringBoot的默认的配置文件application.properties ...
- 获取配置文件信息——configparser
配置文件host.int格式如下: [host]product=xxxxxxxxxxtest=xxxxxxxxxx python 3.x代码如下: import os,configparser def ...
- .Net FrameWork获取配置文件信息
今天在做项目的时候需要到配置文件中读取信息,之前自己并没有操作过,今天算是完成自己的一个心愿.读取配置文件中的信息并不难,大致思路是: 1.载入配置文件,获取配置文件的实例. 2.获取配置文件的节点 ...
- mybatis整合spring获取配置文件信息出错
描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...
- Spring MVC通过Pageable对象和PageableDefault注解获取分页信息(MongoDB通过Pageable来操作分页)
说明:Pageable同时也能用于操作MongoDB的分页. PageableSpring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息( ...
- Spring Boot使用@ConfigurationProperties注解获取配置文件中的属性值
注意:这种方式要提供属性的getter/setter方法—— 如果idea报错,提示没有相应的执行器,就需要在maven中添加: (虽然不配置代码也能正常运行,作用在下面会说明) 配置了该执行器后,在 ...
- Sping boot 之 @Value("${xxx}") 注解获取配置文件内容
1.注解方式读取 1-1.@PropertySource配置文件路径设置,在类上添加注解,如果在默认路径下可以不添加该注解. 需要用@PropertySource的有: 例如非application. ...
随机推荐
- android system.img 解压和打包
system.img重新编译的时间太长,添加和更改的文件系统内容,往往通过对system.img加压再打包的方式. 参考链接 http://blog.csdn.net/whu_zhangmin/art ...
- Servlet采用多线程来处理多个请求同时访问
Servlet采用多线程来处理多个请求同时访问,Servlet容器维护了一个线程池来服务请求.
- 5.3 SpEL语法
SqEL是一个可以独立于spring的表达式语言,即它可以用在XML中对语法进行简化 5.3 SpEL语法5.3.1 基本表达式一.字面量表达式: SpEL支持的字面量包括:字符串.数字类型(int. ...
- ubuntu 解压命令全部
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...
- php -- 判断文件是否存在
file_exists is_file is_dir 基本上,PHP的 file_exists = is_dir + is_file 写程序验证一下: 分别执行1000次,记录所需时间. ------ ...
- 【cb2】扩展硬盘
1.硬盘为sata串口 2.参考 http://docs.cubieboard.org/tutorials/ct1/installation/moving_rootfs_from_nandflash_ ...
- 怎么隐藏MathType标尺
因为MathType公式编辑能力非常的好用,所以非常的受大家的欢迎.MathType用现有的模板可以直接输入输出各种公式,而且MathType中有着各式各样的数学符号满足了大家日常公式的需求,为大家的 ...
- uva 610(tarjan的应用)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23727 思路:首先是Tarjan找桥,对于桥,只能是双向边,而对于 ...
- requirejs源码分析,使用注意要点
本文将深度剖析require.js代码,为了是大家更高效.正确的去使用它,本文不会介绍require的基本使用! 概要 先来一个流程图来概要一下大概流程 在require中,根据AMD(Asynchr ...
- 《Windows核心编程》读书笔记.Chapter06线程基础
原文链接在印象笔记(效果也好的多):https://app.yinxiang.com/l/AAQlNLnxTPRMAppVr5W0upchipQDDC_FHlU 概要: 现成也有两个组成部分: 现成的 ...