포스트 네비게이션 -readme
첫째, 봄 부팅 핫 배포를 구성
때문에 기술 사람들의 심리적 발달의 항상 우리가 코드마다 시간을 수정하지 않는 경우, 게으른되고 싶어요, 당신은 서버를 다시 시작해야하며, 코드를 다시 실행합니다. 당신은 모양 뜨거운 배포를 구성 할 수 있습니다. 나중에, 코드를 수정 만 다시 구축 할 필요를 사용하면 효과를 볼 수 있습니다, 당신은 서버를 다시 시작할 필요가 없습니다.
1. 구성 핫 배포
- 의 pom.xml 파일은 다음과 같은 종속성을 추가 :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <optional>true</optional>
</dependency>
- 의 pom.xml 파일을 수정
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--开启热部署-->
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
pom.xml 파일을 수정 한 후, 아이디어는 자동 클릭, 패키지를 가져올 수있는 파일이 나타납니다 Import Changes
. 항아리 패키지는 자동으로 라이브러리를 의존하는 다운로드됩니다.
그래서, 우리의 코드를 수정하려면이 시간에 뜨거운 배포 봄 부팅을 달성하기 위해, 단순히 클릭하면 빌드 다시.
2. 구성 자동 빌드
당신이 더 많은 게으른 있다면 물론, 다음 싶지 빌드 연습, 당신은 아이디어에 (빌드 만 자동으로 봄 부팅 프로젝트 지원) 자동으로 빌드하도록 구성 할 수 있습니다.
- 암 것두 기억 안나 -> 파일 -> 설정을 열고 ... 다음과 같은 인터페이스를 볼 수 있습니다
위의 이미지를 선택Build Project automatical
- 키 조합을 눌러
Shift+ALT+Ctrl+/
선택하려면Registry
다음과 같은 인터페이스를 볼 수 있습니다
위의 이미지를 선택complier.automake.allow.when.app.running
따라서,도를 다시 시작해야 빌드를 다시 할 필요없이, 우리의 코드를 수정.
두, 봄 부팅 구성 파일을 읽고
- 우리의 구성 파일을 수정
application.properties
server.port=8888
<!--网站配置-->
website.name=Loading
website.domin=www.loading.ink
website.title=我的博客网站
website.description=分享我的生活和技术
- 새로운 프로필 클래스
WebSiteConfig
package spring.boot.web.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.text.MessageFormat;
@Configuration
//@ConfigurationProperties(prefix = "website")
//要读取的配置文件地址
@PropertySource(value = "classpath:application.properties")
public class WebSiteConfig {
@Value("${website.title}")
private String title;
@Value("${website.domain}")
private String domain;
@Value("${website.description}")
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return MessageFormat.format("Title:{0} Domin:{1} Description:{2}", title, domain, description);
}
}
두 가지 방법 중 하나를 사용하여 구성 파일을 읽습니다
1 @ConfigurationProperties(prefix = "website")
프리픽스는 프리픽스 결합 속성을 나타내는 결합 속성 특징에 대한. 동일한 구성 및 속성 이름의 구성 파일이이 방법으로 사용할 수있는 경우
2. @Value("${website.title}")
바인딩 구성 파일 속성
참고 :
주석 @Configuration
구성 클래스를 정의
참고 @PropertySource(value = "classpath:application.properties")
구성 파일 인 경우 구성 파일을 읽고 싶은 경로를 대신 application.properties
시간이 주석은 생략 할 수 있습니다
- 새로운
WebSiteController
package spring.boot.web.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import spring.boot.web.config.WebSiteConfig;
@RequestMapping("/website")
@RestController
public class WebSiteController {
//通过@Autowired注解注入bean
@Autowired
private WebSiteConfig webSiteConfig;
@RequestMapping(method = RequestMethod.GET)
public String get() {
return webSiteConfig.toString();
}
}
- 테스트 실행
- 문제가 처음 실행하면 중국어의 구성 파일이 깨져서 읽기가 발생합니다. 구성 파일 형식의 형식은 UTF-8 년대 없기 때문에 주된 이유는 아마이다. 이 경우 아이디어는 제공 될 수있다.
如上图所示更改为uft-8
,注意后面的一定要勾选上。
通过上面配置后如果还不行,可以将配置文件删除后重新建一个,问题就可以解决!
三、Spring Boot Profile
Spring Boot 使用一个全局的配置文件 application.properties
,Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。
在日常开发中,我们常常会遇到一个问题。就是在不同的环境使用不同的配置。比如生产、开发、测试三个不同的环境,我们的配置肯定不一样。这时,我们就要用到Profile。
Profile
是 Spring 用来针对不同的环境对不同的配置提供支持的,全局 Profile
配置使用 application-{profile}.properties
(如 application-dev.properties
)。通过在 application.properties
中设置 spring.profiles.active = dev
来指定活动的 Profile
。
- 依次再目录下面新建三个配置文件,
application-dev.properties
、application-test.properties
、application-prod.properties
。它们分别代表开发环境、测试环境、生产环境的配置文件。
server.port=8887
website.title=我的博客网站--Dev
website.domain=www.loading.ink
website.description=分享我的技术与生活
server.port=8886
website.title=我的博客网站--test
website.domain=www.loading.ink
website.description=分享我的技术与生活
server.port=8885
website.title=我的博客网站--prod
website.domain=www.loading.ink
website.description=分享我的技术与生活
- 接下来修改
application.properties
:表示,将采用application-dev.properties这个配置文件。
spring.profiles.active=dev
测试运行我们可以看到会启动开发环境配置文件的端口8887
注意:配置文件会优先获取Profile
中的配置,如果Profile
中没有的配置项, 那么会直接取application.properties中
的配置