上一篇博客中介绍了如何建立一个简单的Spring boot应用,本篇文章介绍Spring boot中的一些基本配置,只有认识和了解这些配置,才能为咱们之后的深刻学习Spring boot作好铺垫。php
文章首发于我的博客:【www.xiongfrblog.cn】html
首先,咱们在新建一个Spring boot项目的时候,Spring boot会自动为咱们在包的根目录下建立一个名为xxxApplication.java
的启动类,该启动类是咱们项目的入口类,包含一个main
方法,执行该方法就启动了项目。java
启动类中有一个核心的注解@SpringBootApplication
,它是一个组合注解,包含如下三个注解:web
@Bean
使用将某个对象注册到Spring上下文。@Component
,@Service
,@Controller
等标记的类。上边说过启动类通常放在包的根目录下,是由于@ComponentScan注解默认扫描当前包及子包,若是须要指定扫描路径,须要加上参数,例如
@ComponentScan("com.example.demo.dao")
。spring
Spring boot官方配置了@SpringBootApplication
注解来替代上边介绍的三个注解,更加简洁明了。apache
Spring boot使用一个全局的配置文件application.properties
或application.yml
,该配置文件通常位于src/main/resource
目录下,两种配置文件惟一的区别就在于书写的格式不同,如今比较主流的是application.yml
格式的配置文件,要熟练使用该种格式的配置文件须要熟悉基础的yaml
语法,这里不作过多介绍,本篇博客均使用application.properties
格式的配置文件,下面介绍两个最基本的配置。浏览器
server.port=1188
复制代码
通常访问项目的根路径默认localhost:1188
,可是有时候咱们会在访问路径上加上当前项目的名字,这时候就须要修改默认的访问路径了。springboot
#spring boot版本2.0如下
server.context-path=/demo
#spring boot版本2.0以上
server.servlet.context-path=/demo
复制代码
这里根据Spring boot的版本不一样有不一样的配置们你们根据本身的版本选择对应的属性名便可,因为个人Spring boot版本是2.1.2,因此选择server.servlet.context-path
的属性名,此时访问项目的根路径就的url就变成了localhost:1188/demo
。bash
固然,Spring boot中能够配置属性还有不少,好比配置 Email:app
spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
spring.mail.host= # SMTP server host. For instance, `smtp.example.com`.
spring.mail.jndi-name= # Session JNDI name. When set, takes precedence over other Session settings.
spring.mail.password= # Login password of the SMTP server.
spring.mail.port= # SMTP server port.
spring.mail.properties.*= # Additional JavaMail Session properties.
spring.mail.protocol=smtp # Protocol used by the SMTP server.
spring.mail.test-connection=false # Whether to test that the mail server is available on startup.
spring.mail.username= # Login user of the SMTP server.
复制代码
这里就不列举了,太多了,具体能够查阅官方文档【传送门】
在实际项目中,不少状况咱们须要定义一些全局属性,在须要的地方注入使用便可,Spring boot容许咱们在application.properties
下自定义一些属性,下面介绍自定义属性而且使用。
blog.login.name=admin
blog.login.pass=1234
复制代码
咱们在application.properties
定义了blog.login.name
和blog.login.pass
两个属性。
为了方便演示,咱们定义一个名为TestController.java
的控制器,使用@Value
注解注入属性,所有代码以下:
package com.web.springbootconfig.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/** * @author Promise * @createTime 2018年12月27日 下午10:11:35 * @description */
@Controller
public class TestController {
@Value("${blog.login.name}")
private String name;
@Value("${blog.login.pass}")
private String pass;
@RequestMapping("/test")
@ResponseBody
public String configTest() {
return "name="+name+";pass="+pass+";nick="+nick;
}
}
复制代码
能够看到使用@Value(属性名名)
的方式注入咱们自定义的属性,此时启动项目浏览器访问localhost:1188/demo/test
或127.0.0.1:1188/demo/test
获得以下结果:
由图能够看出,咱们拿到了在配置文件中自定义属性的值admin
和1234
。
这里有一点须要你们特别注意,只有在被Spring管理的类中才能注入属性,好比上文中咱们的
TestController.java
控制器类名上加了@Controller
注解,代表将此类交由Spring管理。
新建一个Text.java
类,内容以下:
package com.web.springbootconfig.entity;
import org.springframework.beans.factory.annotation.Value;
/** * @author Promise * @createTime 2019年1月14日 下午11:35:52 * @description */
public class Test {
@Value("${blog.login.name}")
private String name;
@Value("${blog.login.pass}")
private String pass;
//省略getter,setter方法
}
复制代码
再在TestController.java
控制器中添加以下方法:
@RequestMapping("/check")
@ResponseBody
public String check() {
Test test =new Test();
return "name="+test.getName()+";pass="+test.getPass();
}
复制代码
重启项目,浏览器访问localhost:1188/demo/check
,此时获得以下结果:
能够看到输出为null
,正是由于咱们的Text.java
类没有交由Spring管理,因此Spring并无为咱们自动注入属性,这点必定要注意!
从以前的操做中,不少小伙伴确定已经发现了咱们每次对项目作修改的时候都须要手动重启项目才可以正确访问到新的内容,这对咱们日常的开发来讲确定很是不友好的,别担忧,Spring boot也为咱们设计了热部署功能,修改完代码保存以后Spring boot会自动重启加载最新的内容,只须要咱们简单的配置便可。
Spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring boot应用支持热部署,提升开发者的开发效率,无需手动重启Spring boot应用。
在pom.xml
文件中添加Devtools
依赖,代码以下:
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
复制代码
而且修改build
模块,以下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
复制代码
在配置文件中添加以下内容(其实不添加也能够,由于默认就是打开的,这里提出来是方便有时候能够关闭热部署。)
spring.devtools.restart.enabled: true
复制代码
须要关闭热部署功能时将属性值设置为false
便可。
好了,至此热部署的功能就已经配置完成了,如今能够启动项目,而后随便修改一个文件保存,你会发现控制台已经更新了日志,等待项目从新启动以后发现更新的内容已经展现在浏览器上了。
在实际的企业级开发过程当中,咱们老是有开发环境,生产环境等不一样的环境,同一个项目在每个不一样的环境须要的配置老是不同的,这时候若是每换一个环境就修改一次application.properties
配置文件的内容,对开发人员来讲是很是糟糕的体验,因此Spring boot为咱们提供了不一样环境指定特定的配置文件的功能,而咱们只须要简单的配置便可,完美解决难题。
多环境配置文件必须以application-{profile}.properties的格式命名,其中{profile}为环境标识,好比咱们有一个开发环境需设置启动端口为1188,一个生产环境需设置启动端口为1189,两个配置文件分别为:
server.port=1188
复制代码
server.port=1189
复制代码
启动项目具体加载哪一个配置文件须要在application.properties
配置文件中添加以下代码指定(好比咱们使用开发环境):
spring.profiles.active=dev
复制代码
Spring boot能够将项目打成不一样类型的包,好比jar包,war包,pom包等,这里咱们只介绍最长用的jar包,打包须要在pom.xml文件中有以下的配置(这里贴出完整的pom.xml):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.web</groupId>
<artifactId>springboot-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
复制代码
其中关于打包的重要代码为:
指定打包的类型
<packaging>jar</packaging>
复制代码
Spring Boot Maven打包插件:spring-boot-maven-plugin
,该插件将项目打成一个可执行的jar包,包括把应用程序的全部依赖打入jar文件内,能让你在命令行用java -jar来运行应用程序。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
复制代码
右键项目选择Run As
->Maven build
,在弹出框的Goals
输入clean package
点击Run
便可。
打包前在项目的根目录下会发现一个空的target
文件夹,打包生成的文件会放在这个文件夹内,打包前:
打包后:
能够看到生成了一个jar文件。
命令行进入jar包所在的目录,运行Java -jar jar包名.jar
便可启动项目,此时启动浏览器访问localhost:1188/demo/test
能够看到与以前同样的内容。
在执行jar包的时候咱们也能够指定参数,好比指定端口号为8089java -jar jar包名.jar --server.port=8089
,也能够指定使用哪一个环境的配置文件等不少东西,这里不一一律述了。
好了,Spring boot的一些基础的配置就说到这里,更多的内容须要咱们本身在不断的实践中继续了解,下期内容再见,bye~