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读取配置文件及自定义配置文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!