SpringBoot项目一般有一个名为*Application的入口类,入口方法为此类的main方法。java
@SpringBootApplication注解是一个组合注解,主要组合了一下注解:mysql
(1)@Configuration : 声明当前类是一个配置类,至关于一个Spring配置的xml文件。意味着这个类里可能有0个或者多个@Bean注解。spring
(2)@EnableAutoConfiguration : 让SpringBoot根据类路径中的jar包依赖为当前项目进行自动配置。sql
(3)@ComponentScan:设置bean扫描的包。数据库
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration
所以,若不使用@SpringBoot注解,则能够在入口类上直接使用这个三个注解。安全
SpringBoot会自动扫描入口类所在的同级包以及下级包里的bean。微信
经过@SpringBootApplication注解的参数exclude参数实现:app
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
在SpringBoot启动时会有一个默认启动图案:微信公众平台
(1)修改Banneride
在src/main/resources 下新建一个banner.txt
//////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : |||// \ // // / _||||| -:- |||||- \ // // | | \\\ - /// | | // // | \_| ''\---/'' | | // // \ .-\__ `-` ___/-. / // // ___`. .' /--.--\ `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;`\ _ /`;.`/ - ` : | | // // \ \ `-. \_ __\ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 永不宕机 永无BUG // ////////////////////////////////////////////////////////////////////
也能够去 http://patorjk.com/software/taag/ 在线生成字符,而后将生成的字符复制到banner.txt文件中便可。
(2)关闭Banner
在入口文件Application.java的main方法中,修改成:
SpringApplication application = new SpringApplication(Application.class); application.setBannerMode(Banner.Mode.OFF); application.run(args);
SpringBoot提倡零配置,即无XML配置,但实际上有些特殊的配置须要使用XML配置。咱们能够经过Spring提供的@ImportResource来加载XML配置
@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})
SpringBoot默认的配置文件名称为 application.properties,
默认搜索路径为:
file:./ // 当前目录下的/config子目录,
file:./config/ // 当前目录
classpath:/ // classpath根路径
classpath:/config/ //classpath下的/config目录
加载顺序按优先级排序的(列表中位置高的将覆盖位置低的)。
注:Spring-boot配置文件的加载,
先在与jar同级下查找,
若是没有就去同级的config下查找;
若是再没有,就在jar包中去查找相应的配置文件,
若是再没有,就去jar包中的config下去查找。当查找到对应配置片断时,采用增量替换的方式来进行替换。
具体可见源码 ConfigFileApplicationListener :
Profile为在不一样环境下使用不一样的配置提供了支持(如开发环境和生成环境下数据库的配置)
若启用dev开发环境配置,则须要在application.properties配置文件中,配置如下属性
spring.profiles.active=dev
则,SpringBoot除了加载application.properties配置文件外,还会加载开发环境的application-dev.properties配置文件,以下:
# Mysql 注意替换相应配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/smart-blog spring.datasource.username=root spring.datasource.password=root
(1)端口
server.port=9090
(2)应用名
server.servlet.context-path=/weixin-service。
SpringBoot能够基于jar包运行,打成jar包的程序能够直接经过下面的命令行运行,并修改Tomcat端口号:
java -jar xx.jar --server.port=9090
即@Value方式
(1)在application.properties中增长属性:
book.author=wangyunfei book.name=Spring.boot
(2)在须要使用属性的类中,如入口类:
@RestController @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class Application { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @RequestMapping("/") public String index() { return "book author is:" + bookAuthor+ " and book name is: " + bookName ; } public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
即基于properties
@Value须要将属性一个一个的注入,当属性较多时,会比较麻烦。这时就能够经过 @ConfigurationProperties将properties 属性和一个Bean及其属性关联,从而实现安全配置。
(1)application.properties配置文件
在application.yml上添加(这里以yml格式为例),:
#微信公众平台配置 weixin: qy: corpId: ww92f5da92234696e agentSecret: I73733ve233s6H_ijPvIjYD4Rese5UlbYhhQOEE1-I contactsSecret: 1m_9XP62YrXjSiYtL5Th323rqaExKfr_5eAL09w agentId: 1000002 token: ray encodingAesKey: z2W9lyOAR1XjY8m2323qib0TlBZzCFiCLp6IdS2Iv state: hec4
咱们也能够新建一个yml文件,这时咱们须要使用@PropertiesSource将添加的文件的位置指定。
(2)配置类
package com.ray.weixin.qy.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author : shira * @date : 2018/4/17 * @time : 22:33 * @desc : **/ @Data @Component @ConfigurationProperties(prefix = "weixin.qy") public class WeiXinAuthConfig { private String corpId; private String agentSecret; private String contactsSecret; private int agentId; private String token; private String encodingAesKey; private String state; }
经过 @ConfigurationProperties 加载文件内的配置,经过prefix属性指定properties的配置的前缀。
而后在须要用的配置信息的时候,直接注入这个类就能够了。
1.《Java EE 开发的颠覆者—SpringBoot 实战》,汪云飞