spring boot 项目总结

1 spring boot 项目构建

1.1 pom 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
  <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>

    <artifactId>spring-boot-example</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>spring-boot-example</name>
    <description>spring boot example </description>


    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <finalName>spring-boot-example</finalName>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.2 启动类

@RestController
@SpringBootApplication
public class SpringBootBootStrapExample {

    public static void main(String[] args) {
		SpringApplication.run(SpringBootBootStrapExample.class, args);
    }


    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }


}

2 spring boot 部署注意事项

2.1 spring-boot 项目 war包启动

1 修改打包方式为 war

<packaging>war</packaging>

2 启动类继承SpringBootServletInitializer 并Override configure方法 如下

public class SpringBootBootStrapExample extends SpringBootServletInitializer {
    
    public static void main(String[] args) {
        SpringApplication.run(SpringBootBootStrapExample.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringBootBootStrapExample.class);
    }

3 将 spring-boot-starter-tomcat 依赖改为provided

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<scope>provided</scope>
</dependency>

2.2 spring-boot 热部署

1 添加依赖 spring-boot-devtools 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

2 设置不重启

public static void main(String[] args) {
    System.setProperty("spring.devtools.restart.enabled", "false");
    SpringApplication.run(MyApp.class, args);
}

3 spring boot 注入bean

3.1 注入Servlet,Filter,*Listener

3.1.1 通过 *RegistrationBean

ServletRegistrationBeanFilterRegistrationBean, ServletListenerRegistrationBean

Servlet ServletRegistrationBean

    @Bean
    public ServletRegistrationBean myServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new MyServlet());
        servletRegistrationBean.addUrlMappings("/service");
        return servletRegistrationBean;
    }

Filter FilterRegistrationBean

    @Bean
    public FilterRegistrationBean myFileter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

Listener ServletListenerRegistrationBean

    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
        servletListenerRegistrationBean.setListener(new MyListener());
        return servletListenerRegistrationBean;
    }
3.1.2 embedded containe 嵌入容器 (tomcat jetty) jar启动

注: war 启动不适用

@WebServlet, @WebFilter, and @WebListener

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值