1. Maven工程,在pom
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </依賴性> <! - > <依賴項> <groupId> org.springframework.boot </groupId> <ARTIFACTID> spring-boot-starter-starter-test </artifactId> </artifactid> </depentency> <groupenty> <groupId> <groupId> junit> junit> junit> </groupid>
2.創建項目啟動類startapplication.java
包com.kelly.controller; import org.springframework.boot.springapplication; import org.springframework.boot.boot.autoconfigure.enableautoconfiguration; import org.springframework.springframework.context.context.annotext.annotation.componentscan; import emport; import; import; org.springframework.context.annotation.configuration;@configuration@enableAutoConfiguration /////自動加載配置信息@@componentscan(“ com.kelly”)//////////////////////////////////////////////- }}}包com.kelly.controller; import org.springframework.boot.springapplication; import org.springframework.boot.boot.autoconfigure.enableautoconfiguration; import org.springFringframeWork.context.Context.Context.Annotation.components.components; importcun; importcun; org.springframework.context.annotation.configuration;@configuration@enableAutoConfiguration /////自動加載配置信息@@componentscan(“ com.kelly”)//////////////////////////////////////////////- }}}} package com.kelly.controller; import org.springframework.beans.factory.annotation.value; import org.springframework.stringframework.stereotype.stereotype.controller; import org.springframework.web.web.bind.annot.ant.annotation.request.requestmappation emptigmpappe; import; import; import; import; import; import; org.springframework.web.bind.annotation.responsebody; @controllerPublic class firstController {@value(“ $ {test.name}”)私有字符串名稱; @value(“ $ {test.password}”)私有字符串密碼; @requestmapping(“/”)@Responsebody字符串home(){返回“ Hello Springboot!”; } @requestmapping(“/hello”)@Responsebody字符串hello(){return“ name:“ + name +”,“” +“ password:” + password; }}}5.打開瀏覽器,輸入http:// localhost:8081/springboot/hello即可看到結果
6.使用java bean的方式讀取自定義配置文件define.properties
defineentity.java
軟件包com.kelly.entity; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.context.context.annotext.annotation.propertation.propertysource; import; import; import; import; org.springframework.stereotype.component;@component@configurationProperties(prefix =“ defenetest”)@propertySource(“ classpath:define.properties”)public class clublic class defineentity {private string string pname pname;私人字符串密碼; public String getPname(){return pname; } public void setPname(string pname){this.pname = pname; } public String getPassword(){返回密碼; } public void setpassword(字符串密碼){this.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;import org.springframework.web.bind.annotation.responsebody; import com.kelly.entity.defineentity; @controllerpublic class secondController {@autowied defineentity defineentity defineentity defineentity; @requestmapping(“/define”)@Responsebody字符串define(){return“ test.name:” + defineentity.getPname() +“,test.password:” + defineentity.getPassword(); }}}7.打開瀏覽器,訪問http:// localhost:8081/springboot/define,可以看到輸出結果
補充:我的項目的目錄結構
總結
以上所述是小編給大家介紹的Springboot讀取配置文件及自定義配置文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持! ,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!