可以使用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;