Spring boot document Study Notes day02 and Study Notes day02
1.Start to report an error. View debug
$ Java-jar myproject-0.0.1-SNAPSHOT.jar-debug
2.Custom banner
The banner that is printed on start up can be changed by addingbanner.txt
File to your classpath or by settingspring.banner.location
Property to the location of such a file. If the file has an encoding other than UTF-8, you can setspring.banner.charset
. In addition to a text file, you can also addbanner.gif
,banner.jpg
, Orbanner.png
Image file to your classpath or setspring.banner.image.location
Property. Images are converted into an ASCII art representation and printed above any text banner.
3.Custom SpringApplication
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
4.Get running parameters
If you need to access the application arguments that were passedSpringApplication.run(…
)
, You can injectorg.springframework.boot.ApplicationArguments
Bean.ApplicationArguments
Interface provides access to both the rawString[]
Arguments as well as parsedoption
Andnon-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.Read order of 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.Specify the configuration file name and location
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 loose multiple bindings. It is recommended to use lowercase + hyphen in properties.
Public class OwnerProperties {
PrivateString firstName;
PublicString getFirstName (){
Returnthis. firstName;
}
Public voidsetFirstName (String firstName ){
This. firstName = firstName;
}
}
In thepreceding 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. |
Theprefix
Valuefor the annotationMustBe in kebab case (lowercase and separated-
, Suchacme.my-project.person
).
8.Profiles
SpringProfiles provide a way to segregate parts of your application configuration andmake it be available only in certain environments. Any@Component
Or@Configuration
Can be marked@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