上一篇Spring Boot咱们简单讲了如何快速建立一个SpringBoot项目。你们都知道SpringBoot很是强大,能够轻松与各类工具集成,可是咱们知其然,也必须知其因此然。今天开始就和你们一块儿学习一下SpringBoot核心,核心因为过于重要,须要分红好几章,今天咱们先来看看基本配置。
使用过或者瞄过一眼Spring Boot工程的小伙伴都知道, SpringBoot有一个特别显著的特色, 就是每一个SpringBoot工程都会有一个入口类, 在这个入口类上都会有这么一个注解@SpringBootApplication。 这个类中有一个main方法,main方法中使用 SpringApplication.run(*Application.class,args), 用来启动SpringBoot项目。以下所示:
public static void main(String[] args) { SpringApplication.run(Createproject2Application.class, args); }
@SpringBootApplication是Spring Boot的核心注解, 它是一个组合注解 (一般咱们称由多个注解组成的注解叫组合注解)。点进去瞧一眼
@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 {
@SpringBootApplication注解主要(我这里说了主要喔)组合了 @Configuration,@EnableAutoConfiguration,@ComponentScan 言外之意就是能够将这三个注解直接替换@SpringBootApplication
- @EnableAutoCOnfiguration让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。例如添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的依赖,Spring Boot就会对Tomcat和Spring MVC进行自动配置。
2.@ComponentScan 让Spring Boot去扫描与入口类同级以及如下包的Bean(使用注解配置的),把他们添加到Spring容器,若为JPA项目还能够扫描标注@Entity的实体类。java
3.@Configuration 表示当前是一个配置类,也会被Spring进行加载web
SpringBoot为咱们提供了自动化配置,可是在某些特定的场景下, 咱们可能不须要某个自动配置, 这时能够在@SpringBootApplication中配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
什么是Banner呢, 就是在Spring Boot项目启动的时候最开始显示的横幅。 我记得我第一次启动Spring Boot项目的时候印象最深的就是这个横幅 心里不断OS(wc这么酷炫的吗)。 下面咱们看看怎么自定义横幅吧。若是不自定义,默认显示下面图案
固然了,这个banner也不是必需要显示的,咱们能够手动关闭它。
SpringApplication app = new SpringApplication(Createproject2Application.class); app.setBannerMode(Banner.Mode.OFF); app.run(args);
Spring Boot在src/main/resources下有一个全局的配置文件 application.properties或application.yml 说到yml这种配置文件,是有点东西的。全称为yaml,是以数据为中心, 支持多种数据格式(如数组),在配置数据的时候具备面向对象的特征。
在两种配置文件中配置Tomcat的端口号和默认的访问路径
server.port=8888 server.servlet.context-path=/xicent
server: port: 8888 servlet: context-path: /xicent
其实咱们从简单的实例中能够看出,yaml的格式更加清晰,全部配置看过去一目了然。而且它是有序的。
在之前idea中是不支持yml提示的,如今也都支持了。
可是yaml却引来了另外一个问题,yaml格式有严格的要求,稍有配错都有可能出现问题。
所以在项目中看咱们如何去取舍了,目前默认仍是以properties为主。spring
Spring Boot提倡的是0配置,即无xml配置,可是在实际开发中, 咱们有时不免会须要加载xml配置, 这时咱们就能够经过Spring提供的@ImportResource来加载xml配置 例如:
@ImportResource({"classpath:some-context.xml"})
这样咱们就成功加载xml配置啦。
在Spring Boot中,咱们大部分配置都写在了配置文件中, 可是有些配置咱们可能须要启动时才能肯定, 所以Spring Boot还提供了一种命令行配置方式 下面演示如何在运行jar包的时候,配置Tomcat的端口号
java -jar xx.jar --server.port=8888
在常规的Spring环境中,若是咱们想加载某个properties文件, 获取其中的配置。一般的作法是在类上加注解@PropertiesSource() 指定配置文件的位置。 而后在类中使用@Value()加载属性。 在Spring Boot中, 咱们只需在application.properties中定义属性, 直接用@Value注入便可。
1.application.properties增长属性segmentfault
xicent.author=kris xicent.age=1
2.修改入口类数组
@Value("${xicent.author}") String name; @Value("${xicent.age}") int age; @RequestMapping("/") String index(){ return "author is"+name+",age is"+age; }
通用咱们用@Value都是获取properties配置文件中配置的属性, 可是@Value的功能可不远远不止这一点喔。 经过@Value注解, 咱们还能获取系统属性,url,随机数,文字流等等。
// 普通字符串 @Value("xicent") private String str; // 操做系统名称 @Value("#{systemProperties['os.name']}") private String osName; // 随机数 @Value("#{T(java.lang.Math).random()*168.0}") private double randomNumber; // 其余bean的属性 @Value("#{demoService.another}") private String fromAnother; // 获取文件资源 @Value("classpath:banner.txt") private Resource file; // 获取地址资源 @Value("http://www.baidu.com") private Resource url; public void testValue() throws IOException { System.out.println(getStr()); System.out.println(getOsName()); System.out.println(getRandomNumber()); System.out.println(getFromAnother()); System.out.println(IOUtils.toString(file.getInputStream(),"UTF-8")); System.out.println(IOUtils.toString(url.getInputStream())); } //省略getter,setter方法
访问接口安全
@RequestMapping("/testvalue") void testValue() throws IOException { xicentBean.testValue(); }
上面的例子,咱们每一个属性都要使用@Value注解会显得格外的麻烦, 咱们配置的属性一般会是许多个。 在Spring Boot中使用@ConfigurationProperties 将配置与bean相关联, 这就是所谓的类型安全的配置。 这里将配置配在一个专门的properties文件中, 固然也能直接配置在application.properties中
1.resources文件夹下新增xicent.properties文件,添加以下属性app
xicent.author=kris xicent.age=1
2.建立一个类dom
@Component @PropertySource("classpath:xicent.properties") @ConfigurationProperties(prefix = "xicent") public class XicentBean { private String author; private int age;
代码解释:@PropertySource能够指定咱们须要加载的文件的路径。@ConfigurationProperties指定咱们属性配置的前缀
3.建立接口访问ide
@Autowired XicentBean xicentBean; @RequestMapping("/xicent") XicentBean getXicent(){ return xicentBean; }
4.请求接口spring-boot
Profile是Spring用来针对不一样环境使用不一样的配置文件。 通常命名为:application-{profile}.properties (如application-prod.properties)。 而后在application.properties中 设置spring.profiles.active=prod来指定活动的Profile。 下面演示生产环境(prod)使用8888端口, 开发环境(dev)使用9999端口
1.建立application-prod.properties,配置生产环境的端口
server.port=8888
2.建立application-dev.properties,配置开发环境的端口
server.port=9999
3.application.properties中指定生效的profile
spring.profiles.active=prod
4.启动项目,能够看到prod配置文件生效了,绑定端口为8888
疑问:若是我application.properties和application-prod.properties都配了端口,哪一个会生效呢? 答案是prod的会生效
ok,今天就暂时分享这么多啦,以上讲的是Spring Boot中的基本配置,其中有不少地方都是能够深挖单独拿出来说的。
今天这里只讲了一些基本的,比较经常使用的基本配置,后续咱们还会再详细分享。
喜欢的小伙伴能够关注公众号:喜讯XiCent 有任何问题能够随时问我喔~