1. 啟動報錯,查看debug $ java -jar myproject-0.0.1-SNAPSHOT.jar –debug 2.自定義banner The banner that is printed on start up can be changed by adding a banner.t ...
1. 啟動報錯,查看debug
$ java -jar myproject-0.0.1-SNAPSHOT.jar –debug
2.自定義banner
The banner that is printed on start up can be changed by adding a banner.txt
file to your classpath or by setting the spring.banner.location
property to the location of such a file. If the file has an encoding other than UTF-8, you can set spring.banner.charset
. In addition to a text file, you can also add a banner.gif
, banner.jpg
, or banner.png
image file to your classpath or set the spring.banner.image.location
property. Images are converted into an ASCII art representation and printed above any text banner.
3. 自定義SpringApplication
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
4. 獲取運行參數
If you need to access the application arguments that were passed to SpringApplication.run(…
)
, you can inject a org.springframework.boot.ApplicationArguments
bean. The ApplicationArguments
interface provides access to both the raw String[]
arguments as well as parsed option
and non-option
arguments, as shown in the following example:
import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
}
}
5.application.properties的讀取順序
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
6. 指定配置文件名和位置
The following example shows how to specify a different file name:
$ java -jar myproject.jar --spring.config.name=myproject
The following example shows how to specify two locations:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
7.@ConfigurationPropertie寬鬆的多種綁定,properties中推薦小寫+中劃線
- @ConfigurationProperties(prefix="acme.my-project.person")
public class OwnerProperties {
private String firstName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
In the preceding example, the following properties names can all be used:
acme.my-project.person.firstName |
Standard camel case syntax. |
acme.my-project.person.first-name |
Kebab case, which is recommended for use in .properties and .yml files. |
acme.my-project.person.first_name |
Underscore notation, which is an alternative format for use in .properties and .yml files. |
ACME_MYPROJECT_PERSON_FIRSTNAME |
Upper case format, which is recommended when using system environment variables. |
The prefix
value
for the annotation must
be in kebab case (lowercase and separated by -
, such as acme.my-project.person
).
8. profiles
Spring
Profiles provide a way to segregate parts of your application configuration and
make it be available only in certain environments. Any @Component
or @Configuration
can be marked with @Profile
to limit when it is loaded
You can use a spring.profiles.active
Environment
property to specify which profiles are active:spring.profiles.active=dev,hsqldb