springboot使用(一)
来源:互联网 发布:js 算法 编辑:程序博客网 时间:2024/06/16 00:49
例子均来自官网
官网有很多和其它 模块接的例子,如果用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@EnableAutoConfigurationpublic 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: /myprojectspring: 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; }}
使用:
@AutowiredFooProperties 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
0 0
- springboot使用(一)
- springboot的使用一
- SpringBoot(一) 使用Eclipse搭建SpringBoot开发环境
- 使用springBoot实现 web后端(一)
- 一、springboot项目的简单使用
- springboot中使用spring -data-jpa (一)
- SpringBoot学习与使用一:快速入门
- SpringBoot 一
- SpringBoot(一)
- springBoot(一)
- SpringBoot一
- springboot使用
- SpringBoot小白教程 (一): 使用Maven新建SpringBoot工程
- SpringBoot小白教程 (一): 使用Maven新建SpringBoot工程
- SpringBoot(一)_Eclipse的安装和使用
- springboot学习(一) 官方文档入门及使用
- springboot系列(一):初次使用与登录验证实现
- (一)由浅入深学习springboot中使用redis
- 旋转数组的最小数字
- 美团点评2017秋招笔试真题A
- [BZOJ4196][NOI2005]软件包管理器(链剖+dfs序)
- inotify配置使用详解
- 设计模式(五)原型模式
- springboot使用(一)
- 最近在做关于机械臂的控制
- 用两个栈实现队列
- Java多线程
- leetcode 142. Linked List Cycle II
- Qt信号槽与观察者模式
- JavaWeb——Listener监听器
- PID
- CSS:text-transform