1.创建maven工程、在ポン
<parent> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <バージョン> 1.5.9.9.9.9.9.9.9.9.9.9.9.9.9.9.9.9.9.9.parent> <dependencies> <deplency> org.springframework.boot </dependency> <! - 单元测试使用 - > <依存関係> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> </dependency> <dependency> junit </groupid> <artifactid> junit </docentid> </scope> </scope </scope </scope </scope>
2. startapplication.java
パッケージcom.kelly.controller; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.enableautoconfiguration; Import org.springframework.context.annotation.componentscan;インポートorg.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // }} com.kelly.controller; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.annotation.componentscan;インポートorg.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // }} com.kelly.controller; Import org.springframework.beans.factory.annotation.value; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; Import; org.springframework.web.bind.annotation.responsebody; @controllerpublic class firstcontroller {@value( "$ {test.name}")プライベート文字列名; @value( "$ {test.password}")プライベート文字列パスワード。 @RequestMapping( "/")@responsebody string home(){return "hello springboot!"; } @requestMapping( "/hello")@responsebody string hello(){return "name:" + name + "、" + "password:" + password; }}5.打开浏览器、输入http:// localhost:8081/springboot/hello即可看到结果
6.
defineentity.java
パッケージcom.kelly.entity; Import org.springframework.boot.context.properties.configurationProperties; Import org.springframework.context.annotation.propertysource; Import org.springframework.stereotype.component;@component@configurationProperties(prefix = "defientest")@propertySource( "classpath:define.properties")public class defineThy {private string pname;プライベート文字列パスワード。 public string getpname(){return pname; } public void setpname(string pname){this.pname = pname; } public string getPassWord(){パスワードを返します。 } public void setPassword(string password){this.password = password; }} secondController.javapackage com.kelly.controller; Import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; Import org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.responsebody; import com.kelly.entity.defineentity; @controllerpublic class secondController {@autowired defineEntity defineTive; @RequestMapping( "/define")@responsebody string define(){return "test.name:" + defineEntity.getPname() + "、test.password:" + defineEntity.getPassWord(); }}7.打开浏览器、访问http:// localhost:8081/springboot/define、可以看到输出结果
补充:我的项目的目录结构
总结
以上所述是小编给大家介绍的スプリングブート读取配置文件及自定义配置文件的方法、希望对大家有所帮助、如果大家有任何疑问请给我留言、小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!