您的位置:首页 > 编程语言 > Java开发

Springboot入门(二)项目配置

2017-08-10 18:08 316 查看

pom.xml文件相关信息

<!-- 项目的相关信息-->
<groupId>com.example</groupId>
<artifactId>Test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Test</name>
<description>Demo project for Spring Boot</description>
<!--SpringBoot的parent配置  -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--Springboot web配置  -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Springboot的单元测试  -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--SpringBoot 构建项目时使用的插件配置  -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


TestApplication.java

//要启动springboot项目必须添加该注解
@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
//springboot的启动
SpringApplication.run(TestApplication.class, args);
}

}


HelloWorld.java

1、在类上面条件声明@RestController

2、增加方法@RequestMapping(“/hello”)

@RestController
public class HelloWorld {

@RequestMapping(value="/hello",method=RequestMethod.GET)
public String say() {
return "Hello Springboot";
}
}


application.properties

server.port=8081
server.context-path=/Test


server.context-path=/Test为浏览网址添加了一个路径

访问地址:http://127.0.0.1:8081/Test/hello



这里推荐使用application.yml文件,application.yml文件写法上更加简单。



server:
port: 8082
context-path: /Test


1、注意port:跟8082之间有空格,context-path也是一样有空格。

2、使用yml文件必须进行配置

<!-- 支持 @ConfigurationProperties 注解 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>


例如:

@RestController
public class HelloController {
@Value("${age}")
private Integer age ;
@RequestMapping(value="/helloctrl", method=RequestMethod.GET)
public String say() {
return age+"";
}
}


application.yml

server:
port: 8081
age: 18




Demo

HelloController.java

@RestController
public class HelloController {
@Value("${age}")
private Integer age ;

@Value("${name}")
private String name;

@Value("${content}")
private String content;
@RequestMapping(value="/helloctrl", method=RequestMethod.GET)
public String say() {

return content;
}
}


application.yml

server:
port: 8081
age: 18
name: 小芳
content: "name:${name} age:${age} "




简化书写方式:

PersonProperties.java

//添加组件
@Component
//设置前缀是person
@ConfigurationProperties(prefix="person")
public class PersonProperties {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

}


application.yml

server:
port: 8081
person:
age: 19
name: 小芳


调用

@RestController
public class HelloController {
@Autowired
private PersonProperties mPersonProperties;

@RequestMapping(value="/helloctrl", method=RequestMethod.GET)
public String say() {

return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge();
}
}


结果图省略

开发环境与测试环境配置



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: