您的位置:首页 > 其它

外部属性文件的使用

2017-02-20 14:48 190 查看
可以使用properties文件或yml文件,这里以yml文件为例

application.yml

server:


port: 9090


context-path: /sbapp




local:


name: Zzz


注意,:冒号与值之间要有一个空格

属性文件注入

package com.wisely.bean;




import org.springframework.beans.factory.annotation.Value;


import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.PropertySource;


import org.springframework.stereotype.Component;




/**


* Created by sherry on 17/2/16.


*/


@Component


@PropertySource("application.yml")


public class ReadYml {




@Bean("port")


public String port(@Value("${server.port}") String port){


return port;


}




@Bean("name")


public String name(@Value("${local.name}") String name){


return name;


}




}


引用

@Autowired


@Qualifier("name")


private String name;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: