app-interface.url=http://192.168.2.179:8080/v1/
@EnableConfigurationProperties({AppInterface.class}) @SpringBootApplication public class AppServerApplication { public static void main(String[] args) { SpringApplication.run(AppServerApplication.class, args); } }
@Component @ConfigurationProperties(prefix = "app-interface") public class AppInterface { // 暫時不支持 public static String url; // ="http://192.168.2.179:8080/v1/"; public static String getUrl() { return url; } public static void setUrl(String url) { AppInterface.url = url; } public AppInterface() { } }
1.能夠直接使用AppInterface.url使用 2.能夠@Autowired AppInterface appInterface;將AppInterface注入使用java
@Component public class BlogProperties { @Value("${app-interface.url}") private String appUrl; //省略get,set方法和構造器 }
這種方法比較簡單,適合單個屬性值的注入spring