import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author tracyLi
* @create 2018-08-15 0:57
* 来标注一个主程序类,说明是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWoldApplication {
public static void main(String[] args) {
//Spring应用启动起来
SpringApplication.run(HelloWoldApplication.class,args);
}
}
Controller层
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author tracyLi
* @create 2018-08-15 1:03
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
配置文件
《?xml version=”1.0” encoding=”UTF-8”?>
《project xmlns=”http://maven.apache.org/POM/4.0.0”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd“>
《modelVersion>4.0.0
《groupId>com.atguigu</groupId>
《artifactId>spring-boot-01-helloword</artifactId>
《version>1.0-SNAPSHOT</version>
《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>
《exclusions>
《exclusion>
《groupId>org.springframework.boot</groupId>
《artifactId>spring-boot-starter-logging
《/artifactId>
《/exclusion>
《/exclusions>
《/dependency>
《dependency>
《groupId>org.springframework.boot</groupId>
《artifactId>spring-boot-starter-log4j2
《/artifactId>
《/dependency>
《/dependencies>
<!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
一个简单的Spring Boot 部署完毕!编译问题,把《替换成<,即可!谢谢!
其他配置文件(可忽略)
yml属性赋值
server:
port: 8081
person:
name: zhangsan
age: 18
boss: false
birthday: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaohao
dog:
name: 小狗
age: 2
properties文件赋值
server.port=8082
person.name=86552@qq.com
person.age=18
person.birthday=1918/12/12
person.boss=false
person.dag.name=xiaogou
person.dag.age=2
person.lists=a,s,d
person.maps.k1=liuqiang
person.maps.k2=panbo
自动注入:
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author tracyLi
* @create 2018-08-15 23:24
*/
@PropertySource(value = {“classpath:person.properties”})
@Component
//@Validated
@ConfigurationProperties(prefix = “person”)
public class Person {
@Email
// @Value(“${person.name}”)
private String name;
// @Value(“#{11*12}”)
private Integer age;
// @Value(“true”)
private boolean boss;
private Date birthday;
private Dog dag;
// @Value(“${person.maps}”)
private Map