(转)SpringBoot系列—基础配置
2017-07-19 14:13
471 查看
http://blog.csdn.net/jeofey/article/details/73182705
例:
@SpringBootApplication是Spring Boot 的核心注解,它是一个组合注解主要目的是为了开启自动配置,其中@EnableAutoConfiguration是为了让Spring Boot根据类路径中jar包依赖为当前项目进行自动配置。Spring Boot会自动扫描@SpringBootApplication所在同级包以及下级包的bean。想关闭特定的自动配置可以使用@SpringBootApplication注解的exclude参数例如:
例如修改端口和默认访问路径“/”
常规属性配置和类型安全配置
配置文件添加
类型安全的bean
启动类
访问
日志配置
Profile配置
Profile是用来针对不同环境的不同配置文件提供支持,如 application-dev.properties,通过在application.yml指定活动的Profile。
入口类
SpringBoot项目一般在项目的根包目录下会有一个artifactId+Application命名规则的入口类,入口类有一个main方法(标注的Java应用的的入口方法),main方法中使用SpringApplication.run()启动应用项目。例:
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDemo1Application { public static void main(String[] args) { //通过app.set方法可以设定一些启动参数 SpringApplication app = new SpringApplication(SpringBootDemo1Application.class); app.run(); /**或直接启动 * SpringApplication.run(SpringBootDemo1Application.class, args); */ } }
@SpringBootApplication是Spring Boot 的核心注解,它是一个组合注解主要目的是为了开启自动配置,其中@EnableAutoConfiguration是为了让Spring Boot根据类路径中jar包依赖为当前项目进行自动配置。Spring Boot会自动扫描@SpringBootApplication所在同级包以及下级包的bean。想关闭特定的自动配置可以使用@SpringBootApplication注解的exclude参数例如:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
配置文件
spring Boot使用一个全局配置文件application.properties 或 application.yml (yaml语言是以数据为中心的语言,在配置数据的时候具有面向对象的特征)放置在src/main/resources目录下或者类路径的/config下。例如修改端口和默认访问路径“/”
server: port: 8082 context-path: /demo
常规属性配置和类型安全配置
配置文件添加
book: author: xianjj name: AABB
类型安全的bean
package com.example.demo.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-13 13:31. */ @Component @ConfigurationProperties(prefix = "book") public class BookInitBean { private String name; private String author; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
启动类
@RestController @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class SpringBootDemo1Application { @Value("${book.author}") private String bookAuthor; @Autowired private BookInitBean bookInitBean; @RequestMapping("/") String index(){ return "bookAuthor is " + bookAuthor+"; bookName is "+ bookInitBean.getName(); } public static void main(String[] args) { SpringApplication app = new SpringApplication(SpringBootDemo1Application.class); app.run(); } }
访问
日志配置
logging: file: D:/mylog/log.log level: debug
Profile配置
Profile是用来针对不同环境的不同配置文件提供支持,如 application-dev.properties,通过在application.yml指定活动的Profile。
spring: profiles: active: dev
相关文章推荐
- springBoot基础系列--properties配置
- springBoot基础系列--properties配置
- SpringBoot系列(1)---无配置文件配置基础1
- SpringBoot系列(2)---无配置文件配置基础2
- springBoot基础系列--properties配置
- Java Web系列:Spring Boot 基础 (转)
- Spring Boot干货系列:(二)配置文件解析
- Spring Boot系列教程四:配置文件详解properties
- Spring Boot系列(一) Spring Boot介绍和基础POM文件
- spring boot 基础日志配置
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- (转)SpringBoot系列—SSL配置
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- SpringBoot系列——Java配置(SpringMVC配置)
- SpringBoot基础教程2-1-5 日志规范-logback配置详解
- SpringBoot Redis基础配置
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
- spring-boot系列一 之restfull api与多环境配置
- Spring Boot 基础知识学习(二)——配置文件多环境配置
- SpringBoot系列(二)SpringBoot 项目属性配置