大家好,欢迎来到IT知识分享网。
三、项目属性配置
第一种:application.properties
访问http://127.0.0.1:8081/girl/hello
第二种:application.yml
填写属性需要空格,idea中颜色会变
访问网址:http://127.0.0.1:8082/girl/hello
example:
编写属性cupSize,使用value注解注入controller
访问http://127.0.0.1:8080/cup
创建一个类GirlProperties,使用@ConfigurationProperties(prefix = “name”)映射配置参数,在controller中通过@Autowired注入,相应的GirlProperties要加@Component注解
GirlProperties中需给属性添加getter和setter
GirlProperties
package com.tonny; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired GirlProperties girlProperties; @RequestMapping(value="/hello",method= RequestMethod.GET) public String say(){ return "Hello Spring Boot!"; } @RequestMapping(value="/cup",method= RequestMethod.GET) public String cup(){ return girlProperties.getCupSize(); } }
controller
访问127.0.0.1:8080/cup
多配置切换
欢迎进群交流或关注公众号“IT群英汇”
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/124675.html