使用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. ...
随机推荐
- Statement和PreparedStatement都是用来发送和执行SQL语句的
Statement和PreparedStatement都是用来发送和执行SQL语句的 DriverManager管理一组驱动程序
- Cocos2d-X中的Slider控件
Slider控件事实上就是滑块控件.经常使用于音乐中的音量控制,在Windows编程中开发音乐播放器就须要用到滑块控件控制音量 首先在project文件夹下的Resource文件夹中放 在Skider ...
- UEditor API 文档
来源:http://www.e4dai.com/ueditor-api/#ue.editor http://www.e4dai.com/ueditor-api/ UE.Editor 依赖 editor ...
- 怎么用ChemDraw连接两个结构片段
作为最新版的ChemOffice 15.1的核心组件,ChemDraw几乎能解决所有平面化学结构中的绘制问题.如果用户想连接两个分开的结构片段,ChemDraw提供两种连接两个化学结构片段的方法,分别 ...
- leetcode 153: Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 编程之美 set 19 连连看游戏设计
题目 1. 怎么用简单的计算机模型来描述这个问题 ? 2. 怎么判断两个图像是否能相消 3. 怎么求出相同图形之间的最短距离(转弯数最少)? 4. 怎么确定目前处于死锁状态? 如何设计算法来解除死锁? ...
- onSaveInstanceState
我们已经分析过Activity的启动流程,从中也分析了Activity的生命周期.而其中有一个生命周期方法:onSaveInstanceState方法,今天我们主要讲解一下onSaveInstance ...
- 7624:山区建小学(划分dp)
7624:山区建小学 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 政府在某山区修建了一条道路,恰好穿越总共m个村庄的每个村庄一次,没有回路或交叉,任意两个村庄 ...
- 《从零开始学Swift》学习笔记(Day67)——Cocoa Touch设计模式及应用之MVC模式
原创文章,欢迎转载.转载请注明:关东升的博客 MVC(Model-View-Controller,模型-视图-控制器)模式是相当古老的设计模式之一,它最早出现在Smalltalk语言中.现在,很多计算 ...
- jenkins 配置 ssh插件
一.安装SSH插件 系统管理->插件管理,在可选插件下,过滤SSH,找到publish over ssh插件,直接安装(我这里已经安装过了,在已安装选项下可以找到publish over ssh ...