In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use Spring Boot". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Turn off the specified automatic configuration
The @ EnableAutoConfiguration annotation above can be configured automatically based on the specified dependencies. But if you want to turn off an automatic configuration, you need to use the exclude parameter under @ SpringBootApplication to set it. For example, I want to turn off DataSource. The code is as follows:
1@SpringBootApplication (exclude = {DataSourceAutoConfiguration.class})
two。 Custom launch banner
Can meet the enterprise customization of logo or project launch pattern. For example, you can use the following websites to generate:
1 http://patorjk.com/software/taag
2 http://www.network-science.de/ascii/
3 http://www.kammerl.de/ascii/AsciiSignature.php
After generation, the copied pattern is directly put into the newly created banner.txt file. After running the program, the specific display effect is as follows:
3. Global profile
Application.properties or application.yml files are generally used as global configuration files. It can be added in the following directories, except that the order in which it is loaded is different, as follows:
1 under the / config directory of the project root directory
2 under the project root directory
Under the / config directory of the class 3 path
Under four types of paths
For example, you can set the port, request suffix, and so on in the application.properties configuration file. For specific configuration parameters, please refer to Chapter 10 of the official website, Appendices: https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/#common-application-properties.
4. Configure the Web container
In Spring Boot applications, containers such as Tomcat, Netty, Jetty and so on can be built in.
1) configure Tomcat
If spring-boot-starter-web dependencies are added, the project uses Tomcat as the Web container by default.
You can add some configurations for Tomcat, as shown below:
Sink port
2server.port=80
Wrong jump path
4server.error.path
Failure time of session
6server.servlet.session.timeout
Name of the project
8server.servlet.context-path
Encoding, general utf-8
10server.tomcat.uri-encoding=utf-8
11#...
2) configure Jetty
The configuration of embedding Jetty in Spring Boot applications is very simple. Change the Tomcat in spring-boot-starter-web to Jetty. The specific configuration is as follows:
one
two
3 org.springframework.boot
4 spring-boot-starter-web
five
six
7 org.springframework.boot
8 spring-boot-starter-tomcat
nine
ten
eleven
twelve
thirteen
fourteen
15 org.springframework.boot
16 spring-boot-starter-jetty
seventeen
3) configure Undertow
Undertow is a high-performance Web embedded server based on NIO developed by Red Hat Company, which has very good performance. The configuration method is as follows:
one
two
3 org.springframework.boot
4 spring-boot-starter-web
five
six
7 org.springframework.boot
8 spring-boot-starter-tomcat
nine
ten
eleven
twelve
thirteen
fourteen
15 org.springframework.boot
16 spring-boot-starter-undertow
seventeen
5. HTTPS configuration
Using the keytool tool provided by JDK, you can generate a digital certificate with the following commands:
1keytool-genkey-alias httpskey-keyalg RSA-keysize 2048-keystore hello.p12-validity
-genkey means to create a key
-alias httpskey sets the alias of the key
-keyalg RSA indicates that the encryption algorithm used is RSA
-keysize 2048 sets the length of the key
-keystore hello.p12 sets the key storage location
-validity 365 sets the validity time of the key
Then edit it in the application.properties file, and configure it as follows:
1server.ssl.key-store=hello.p12
2server.ssl.key-alias=httpskey
3server.ssl.key-store-password=123456
6. Type safety configuration
As mentioned earlier, our configuration files can use properties configuration and yaml configuration. After the project is started, they will be loaded into the Spring environment. If you want to use configuration information, you can simply use the @ Value annotation.
However, when data is injected into attributes, you need to pay attention to security. Spring Boot uses type-safe configuration properties, and it is convenient to inject data from configuration files into Bean, even when the amount of data is very large.
1user.name = Cuihua
2user.age=18
3user.addressBeijing
Add the corresponding Bean class, use the @ ConfigurationProperties annotation to use the configuration, and use the prefix attribute to describe the prefix of the configuration file to be loaded, as shown below:
1@Component
2@ConfigurationProperties (prefix= "user")
3public class User {
4 private String name
5 private Integer age
6 private String address
7 / / get and set methods.
8}
7. YAML configuration
YAML is a very concise and powerful language for writing configuration files, similar to JSON. It can be used to replace application.properties files. YAML is mainly parsed by the snakeyaml dependency in the spring-boot-starter-web dependency module. However, it cannot use the @ propertySource annotation to load the YAML file, otherwise it will use the Properties configuration.
To give a small example, it is written as follows:
1server:
2 port:80
3 servlet:
4 context-path:/hello
5 tomcat:
6 uri-encoding:utf-8
You can also customize the configuration, as follows:
1user:
2 name: Cuihua
3 age:18
The corresponding code is the same as the previous User class. The specific source code is as follows:
1@Component
2@ConfigurationProperties (prefix= "user")
3public class User {
4 private String name
5 private Integer age
6 / / get and set methods.
7}
It can also be styled as a collection, where there is a single value, as follows:
1user:
2 name: Cuihua
3 age:18
4 aihao:
5-perm
6-pinch the foot
7-Reading
The corresponding code. The specific source code is as follows:
1@Component
2@ConfigurationProperties (prefix= "user")
3public class User {
4 private String name
5 private Integer age
6 private List aihao
7 / / get and set methods.
8}
You can also set the style of the collection first, but there are objects in the collection, as follows:
1shop:
2 users:
3-name: Cuihua
4 age:18
5 aihao:
6-perm
7-pinch the foot
8-Reading
9-name: Xiao Qiang
10 age:18
11 aihao:
12-perm
13-pinch your feet
14-Reading
The corresponding code. The specific source code is as follows:
1@Component
2@ConfigurationProperties (prefix= "shop")
3public class Users {
4 private List users
5 / / get and set methods.
6}
1public class User {
2 private String name
3 private Integer age
4 private List aihao
5 / / get and set methods.
6}
8. Profile configuration
If we need to frequently change a large number of configurations in the development environment, test environment, and production environment in our project, it will make you suspicious of life, so we use the @ Profile annotation to be more concise.
Specific naming convention: application- {xxx} .properties, so that configuration information can be distinguished in different environments. The specific steps for use are as follows:
First step, add a profile
In the resources directory, create the application-dev.properties and application-prod.properties files that represent the configuration in the development and production environment.
The development environment
2server.port=8080
Production environment
2server.port=80
Second, specify the corresponding mode
1) specify in application.properties
Dev is used for development and prod for production
2spring.profiles.active=dev
2) specify in the startup class main method
1SpringApplicationBuilder builder = new SpringApplicationBuilder (SpringBootApp.class)
two
3builder.application () .setAdditionalProfiles (prod)
four
5builder.run (args)
3) can also be configured at the start of the project
After we type the project into a jar file and start it, the specific operation commands are as follows:
1java-jar springdemo-xxx.jar-- spring.profiles.active=prod comes from: Nai developer community-Jiang Shuai Shuai's "how to use Spring Boot" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.