Spring Boot 有大量可用的工具和配置,可以根据项目的特定需求进行调整。我们将介绍 Spring Boot 中一些有趣的配置选项。
在主要的 Standalone 应用程序中,默认端口号始终为 8080。我们可以在 Spring Boot 中轻松配置端口号。
在 Application.porterties 文件中更改端口,如下所示。
server.port=8088
对于基于 YAML 的配置,在 application.yml 文件中进行如下更改
server:
port: 8088
我们还可以通过编程方式自定义服务器端口:
@Component
public class CustomConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8088);
}
}
您可以选择任何一种端口配置方法。
默认情况下,上下文路径为 “/”
。如果这不理想并且您需要将其更改为 /
app_name
之类的东西,这是通过属性进行的快速简单的方法:
server.servlet.contextPath=/springbootwebapp
对于基于 YAML 的配置:
server:
servlet:
contextPath:/springbootwebapp
我们还可以通过编程方式自定义上下文路径
@Component
public class CustomConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory factory) {
factory.setPort(8088);
factory.setContextPath("/springbootwebapp");
}
}
如果您未在配置中指定任何自定义实现,Spring Boot 会自动注册一个 BasicErrorController
bean。
但是,我们可以配置这个默认控制器。
public class CustomErrorController implements ErrorController {
private static final String ERROR_PATH = "/error";
@GetMapping(value=ERROR_PATH)
public String error() {
return "Error Custom";
}
@Override
public String getErrorPath() {
return ERROR_PATH;
}
}
您可以借助 SpringApplication 以编程方式关闭 Boot 应用程序。 这有一个静态的exit()方法,它接受两个参数:ApplicationContext和ExitCodeGenerator:
@Autowired
public void shutDown(ExecutorServiceExitCodeGenerator exitCodeGenerator) {
SpringApplication.exit(applicationContext, exitCodeGenerator);
}
在 Spring Boot 应用程序中配置日志记录级别非常容易。在配置其他属性时,我们必须在主属性文件中配置日志记录属性。
您可以在 Spring Boot 应用程序中选择任何日志框架,例如 Logback、log4j、log4j2 等。您只需在 POM 文件中定义适当的依赖项
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
如果您在嵌入式服务器的帮助下部署应用程序,您可以在 Boot 应用程序中注册新的 Servlet 通过将它们公开为传统配置中的 bean:
@Bean
public HelloWorldServlet helloWorld() {
return new HelloWorldServlet();
}
或者,您可以使用 ServletRegistrationBean*:***
@Bean
public SpringHelloServletRegistrationBean servletRegistrationBean() {
SpringHelloServletRegistrationBean bean = new SpringHelloServletRegistrationBean(
new SpringHelloWorldServlet(), "/springHelloWorld/*");
bean.setLoadOnStartup(1);
bean.addInitParameter("message", "SpringHelloWorldServlet special message");
return bean;
}
默认情况下,Spring boot 启动器使用 Tomcat 作为默认的嵌入式服务器。如果需要更改 - 您必须排除 Tomcat 服务器依赖项并添加不同的服务器(Jetty、Undertow 等)。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory jettyContainer =
new JettyEmbeddedServletContainerFactory();
jettyContainer.setPort(8088);
jettyContainer.setContextPath("/springbootwebapp");
return jettyContainer;
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory factory =
new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(io.undertow.Undertow.Builder builder) {
builder.addHttpListener(8088, "0.0.0.0");
}
});
return factory;
}
在本教程中,我们介绍了一些有趣的 Spring Boot 配置选项。还有很多可以配置的。请参阅 Spring Boot 文档。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.dailycodebuffer.com/configuring-spring-boot-web-app/
内容来源于网络,如有侵权,请联系作者删除!