1. 工程 Maven 创建 创建, 在 pom 文件中添加依赖
<부모> <groupid> org.springframework.boot </groupid> <artifactid> 스프링-부트-스타터-팔렌트 </artifactid> <bersion> 1.5.9. release </version> </parent> <pexendency> <groupid> org.springframework.boot </groupid> <artifactid> spring-starter-stareb </artifactid </fectionency> <!-> <pectinement> <groupId> org.springframework.boot </groupid> <artifactid> Spring-Boot-Starter-Test </artifactid> </fexpendency> <groupId> junit </groupid> <artifactid> junit> </expence> </scope> </scope> </scope> </scope> </scope> </scope>
2. startApplication.java
package 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 // @componentscan ( "com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public static void main (string [] args) {springApplication.run (startpplication.class, argss); }} package com.kelly.controller; import org.spramepramework.springApplication; import org.springframework.boot.autoconfigure.enableautoconfiguration; import org.springframework.context.annotation.componentscan; 가져 오기 org.springframework.context.annotation.configuration;@configuration@enableautoconfiguration // @componentscan ( "com.kelly") // 使包路径下带有注解的类可以使用 @autowired 自动注入 public static void main (string [] args) {springApplication.run (startpplication.class, argss); }} 패키지 com.kelly.controller; import org.springframework.beans.bean.bean.annotation.value; import org.spramepramework.stereotyp.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. Java Bean 的方式读取自定义配置文件 정의. -Properties
정의. 자바
패키지 com.kelly.entity; import org.springframework.boot.context.properties.configurationProperties; import org.springframework.context.annotation.propertysource; import org.springframework.stereotyp.component;@component@configurationProperties (prefix = "definetest")@propertySource ( "classPath : defin.properties") 공개 클래스 defineentity {private String pname; 개인 문자열 비밀번호; 공개 문자열 getPname () {return pname; } public void setpname (String pname) {this.pname = pname; } public String getPassword () {return password; } public void setpassword (문자열 비밀번호) {this.password = password; }} SecondController.javapackage com.kelly.controller; import org.springframework.beans.ack.annotation.autowired; import org.springframework.stereotyp.controller; import org.spramframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import com.kelly.entity.defineentity; @controllerpublic class secondController {@autowired Defineity Defineentity; @RequestMapping ( "/define") @ResponseBody String define () {return "test.name :" + defineentity.getpname () + ", test.password :" + defineentity.getPassword (); }}7. 访问, http : // localhost : 8081/springboot/define, 可以看到输出结果
补充 : 我的项目的目录结构
总结
SpringBoot ot ot, 希望对大家有所帮助 希望对大家有所帮助, 如果大家有任何疑问请给我留言, 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!