例子均来自官网
官网有很多和其它 模块接的例子,如果用springboot迷茫的时候,这真是一剂良药
spring_boot 最简单的例子
- pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
- Example.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
说明
直接运行Example.java的main函数,
不需要配置文件,springboot会默认,
文中使用了spring-web,spring-mvc, springboot是只要classpath有,它就默认使用了.
启动后直接访问 http://127.0.0.1:8080/
注意 spring-boot-starter-web ,springboot 使用其它的模块,都用这种方式,会有各种各样的starter一些命令行
$ mvn spring-boot:run
- 打成可运行jar包
mvn package
使用:
java -jar target/myproject-0.0.1-SNAPSHOT.jar
自定义 配置
不可能总是用默认,如果要自己配置呢?
程序启动的时候,我们会看到banner
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
如果你不爽这个东西,是可以配置下不出现,可能通过以下来设置
spring:
main:
banner-mode: "on"
既然是个web,想定义一下端口和项目名
server:
port: 8081
context-path: /myproject
那这些配置在哪?官网是用YAML 来配置,
在resources 里加 application.yml
server:
port: 8081
context-path: /myproject
spring:
main:
banner-mode: "on"
这样就可以达到配置的效果了.
也可以通过这种方式配置,少用,但要知道这种链式风格
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Example.class)
.bannerMode(Banner.Mode.OFF)
.run(args);
}
使用配置文件的值
- @Value使用
配置文件application.yml:
env: debug
代码使用:
@Value("${env}")
private String env;
- 如果配置有点复杂可以直接用实体来定:
如
connection:
username: admin
password: admin_pwd
FooProperties 这个类
@Component
@ConfigurationProperties(prefix="connection")
public class FooProperties {
private String username;
private String password ;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
使用:
@Autowired
FooProperties fooProperties;
关于日志,切换log组件
- springboot默认是logback
加上logback.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</layout>
</appender>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
- 如果喜欢用log4j2
1,把pom.xml 改下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
2,新增mylog4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
3,名字叫mylog4j2.xml,肯定不标准,这里手动指定一下
在application.yml中写上
logging:
config: classpath:mylog4j2.xml
相关的代码
https://github.com/huawumingguo/springbootsample/tree/master/springbootbasic