Spring Boot 入门之基础篇(一)

一、前言

Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

本系列以快速入门为主,可当作工具小手册阅读

二、环境搭建

创建一个 maven 工程,目录结构如下图:

# 2.1 添加依赖

创建 maven 工程,在 pom.xml 文件中添加如下依赖:

  1. <!-- 定义公共资源版本 -->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>1.5.6.RELEASE</version>
  6. <relativePath />
  7. </parent>
  8.  
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  12. <java.version>1.8</java.version>
  13. </properties>
  14.  
  15. <dependencies>
  16. <!-- 上边引入 parent,因此 下边无需指定版本 -->
  17. <!-- 包含 mvc,aop 等jar资源 -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. </dependencies>
  23.  
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-maven-plugin</artifactId>
  29. </plugin>
  30. </plugins>
  31. </build>

# 2.2 创建目录和配置文件

创建 src/main/resources 源文件目录,并在该目录下创建 application.properties 文件、static 和 templates 的文件夹。

application.properties:用于配置项目运行所需的配置数据。

static:用于存放静态资源,如:css、js、图片等。

templates:用于存放模板文件。

目录结构如下:

# 2.3 创建启动类

在 com.light.springboot 包下创建启动类,如下:

 
  1. /**
  2. 该注解指定项目为springboot,由此类当作程序入口
  3. 自动装配 web 依赖的环境
  4.  
  5. **/
  6. @SpringBootApplication
  7. public class SpringbootApplication {
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run(SpringbootApplication.class, args);
  11. }
  12. }

# 2.4 案例演示

创建 com.light.springboot.controller 包,在该包下创建一个 Controller 类,如下:

 
  1. @RestController
  2. public class TestController {
  3.  
  4. @GetMapping("/helloworld")
  5. public String helloworld() {
  6. return "helloworld";
  7. }
  8. }

在 SpringbootApplication 文件中右键 Run as -> Java Application。当看到 “Tomcat started on port(s): 8080 (http)” 字样说明启动成功。

打开浏览器访问 http://localhost:8080/helloworld,结果如下:

读者可以使用 STS 开发工具,里边集成了插件,可以直接创建 Spingboot 项目,它会自动生成必要的目录结构。

三、热部署

当我们修改文件和创建文件时,都需要重新启动项目。这样频繁的操作很浪费时间,配置热部署可以让项目自动加载变化的文件,省去的手动操作。

在 pom.xml 文件中添加如下配置:

 
  1. <!-- 热部署 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-devtools</artifactId>
  5. <optional>true</optional>
  6. <scope>true</scope>
  7. </dependency>
 
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. <configuration>
  7. <!-- 没有该配置,devtools 不生效 -->
  8. <fork>true</fork>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </build>

配置好 pom.xml 文件后,我们启动项目,随便创建/修改一个文件并保存,会发现控制台打印 springboot 重新加载文件的信息。

演示图如下:

四、多环境切换

application.properties 是 springboot 在运行中所需要的配置信息。

当我们在开发阶段,使用自己的机器开发,测试的时候需要用的测试服务器测试,上线时使用正式环境的服务器。

这三种环境需要的配置信息都不一样,当我们切换环境运行项目时,需要手动的修改多出配置信息,非常容易出错。

为了解决上述问题,springboot 提供多环境配置的机制,让开发者非常容易的根据需求而切换不同的配置环境。

在 src/main/resources 目录下创建三个配置文件:

 
  1. application-dev.properties:用于开发环境
  2. application-test.properties:用于测试环境
  3. application-prod.properties:用于生产环境

我们可以在这个三个配置文件中设置不同的信息,application.properties 配置公共的信息。

在 application.properties 中配置:

 
  1. spring.profiles.active=dev

表示激活 application-dev.properties 文件配置, springboot 会加载使用 application.properties 和 application-dev.properties 配置文件的信息。

同理,可将 spring.profiles.active 的值修改成 test 或 prod 达到切换环境的目的。

演示图如下:

切换项目启动的环境不仅对读取配置文件信息有效,也可以对 Bean 有效。

当我们需要对不同环境使用不同的 Bean 时,可以通过 @Profile 注解进行区分,如下:

 
  1. @Configuration
  2. public class BeanConfiguration {
  3.  
  4. @Bean
  5. @Profile("dev")
  6. public Runnable test1() {
  7. System.out.println("开发环境使用的 Bean");
  8. return () -> {};
  9. }
  10.  
  11. @Bean
  12. @Profile("test")
  13. public Runnable test2() {
  14. System.out.println("测试环境使用的 Bean");
  15. return () -> {};
  16. }
  17.  
  18. @Bean
  19. @Profile("pro")
  20. public Runnable test3() {
  21. System.out.println("生成环境使用的 Bean");
  22. return () -> {};
  23. }
  24. }

当启动项目后,Spring 会根据 spring.profiles.active 的值实例化对应的 Bean。

五、配置日志

# 5.1 配置 logback(官方推荐使用)

# 5.1.1 配置日志文件

spring boot 默认会加载 classpath:logback-spring.xml 或者 classpath:logback-spring.groovy。

如需要自定义文件名称,在 application.properties 中配置 logging.config 选项即可。

在 src/main/resources 下创建 logback-spring.xml 文件,内容如下:

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <!-- 文件输出格式 -->
  4. <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
  5. <!-- test文件路径 -->
  6. <property name="TEST_FILE_PATH" value="d:/test.log" />
  7. <!-- pro文件路径 -->
  8. <property name="PRO_FILE_PATH" value="/opt/test/log" />
  9.  
  10. <!-- 开发环境 -->
  11. <springProfile name="dev">
  12. <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
  13. <encoder>
  14. <pattern>${PATTERN}</pattern>
  15. </encoder>
  16. </appender>
  17. <logger name="com.light.springboot" level="debug" />
  18. <root level="info">
  19. <appender-ref ref="CONSOLE" />
  20. </root>
  21. </springProfile>
  22.  
  23. <!-- 测试环境 -->
  24. <springProfile name="test">
  25. <!-- 每天产生一个文件 -->
  26. <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  27. <!-- 文件路径 -->
  28. <file>${TEST_FILE_PATH}</file>
  29. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  30. <!-- 文件名称 -->
  31. <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
  32. <!-- 文件最大保存历史数量 -->
  33. <MaxHistory>100</MaxHistory>
  34. </rollingPolicy>
  35. <layout class="ch.qos.logback.classic.PatternLayout">
  36. <pattern>${PATTERN}</pattern>
  37. </layout>
  38. </appender>
  39. <logger name="com.light.springboot" level="debug" />
  40. <root level="info">
  41. <appender-ref ref="TEST-FILE" />
  42. </root>
  43. </springProfile>
  44.  
  45. <!-- 生产环境 -->
  46. <springProfile name="prod">
  47. <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  48. <file>${PRO_FILE_PATH}</file>
  49. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  50. <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
  51. <MaxHistory>100</MaxHistory>
  52. </rollingPolicy>
  53. <layout class="ch.qos.logback.classic.PatternLayout">
  54. <pattern>${PATTERN}</pattern>
  55. </layout>
  56. </appender>
  57. <root level="warn">
  58. <appender-ref ref="PROD_FILE" />
  59. </root>
  60. </springProfile>
  61. </configuration>

其中,springProfile 标签的 name 属性对应 application.properties 中的 spring.profiles.active 的配置。

即 spring.profiles.active 的值可以看作是日志配置文件中对应的 springProfile 是否生效的开关。

# 5.2 配置 log4j2

# 5.2.1 添加依赖

 
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-log4j2</artifactId>
  4. </dependency>
  5.  
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. <exclusions>
  10. <exclusion>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-logging</artifactId>
  13. </exclusion>
  14. </exclusions>
  15. </dependency>

# 5.2.2 配置日志文件

spring boot 默认会加载 classpath:log4j2.xml 或者 classpath:log4j2-spring.xml。

如需要自定义文件名称,在 application.properties 中配置 logging.config 选项即可。

log4j2.xml 文件内容如下:

 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <properties>
  4. <!-- 文件输出格式 -->
  5. <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property>
  6. </properties>
  7. <appenders>
  8. <Console name="CONSOLE" target="system_out">
  9. <PatternLayout pattern="${PATTERN}" />
  10. </Console>
  11. </appenders>
  12. <loggers>
  13. <logger name="com.light.springboot" level="debug" />
  14. <root level="info">
  15. <appenderref ref="CONSOLE" />
  16. </root>
  17. </loggers>
  18. </configuration>

log4j2 不能像 logback 那样在一个文件中设置多个环境的配置数据,只能命名 3 个不同名的日志文件,分别在 application-dev,application-test 和 application-prod 中配置 logging.config 选项。

除了在日志配置文件中设置参数之外,还可以在 application-*.properties 中设置,日志相关的配置:

 
  1. logging.config # 日志配置文件路径,如 classpath:logback-spring.xml
  2. logging.exception-conversion-word # 记录异常时使用的转换词
  3. logging.file # 记录日志的文件名称,如:test.log
  4. logging.level.* # 日志映射,如:logging.level.root=WARN,logging.level.org.springframework.web=DEBUG
  5. logging.path # 记录日志的文件路径,如:d:/
  6. logging.pattern.console # 向控制台输出的日志格式,只支持默认的 logback 设置。
  7. logging.pattern.file # 向记录日志文件输出的日志格式,只支持默认的 logback 设置。
  8. logging.pattern.level # 用于呈现日志级别的格式,只支持默认的 logback 设置。
  9. logging.register-shutdown-hook # 初始化时为日志系统注册一个关闭钩子

六、注解介绍

下面列出 Spring Boot 开发中常用的注解:

 
  1. @Configuration # 作用于类上,相当于一个 xml 配置文件
  2. @Bean # 作用于方法上,相当于 xml 配置中的 <bean>
  3. @SpringBootApplication # Spring Boot的核心注解,是一个组合注解,用于启动类上
  4. @EnableAutoConfiguration # 启用自动配置,允许加载第三方 Jar 包的配置
  5. @ComponentScan # 默认扫描 @SpringBootApplication 所在类的同级目录以及它的子目录
  6. @PropertySource # 加载 properties 文件
  7. @Value # 将配置文件的属性注入到 Bean 中特定的成员变量
  8. @EnableConfigurationProperties # 开启一个特性,让配置文件的属性可以注入到 Bean 中,与 @ConfigurationProperties 结合使用
  9. @ConfigurationProperties # 关联配置文件中的属性到 Bean 中
  10. @Import # 加载指定 Class 文件,其生命周期被 Spring 管理
  11. @ImportResource # 加载 xml 文件

传统项目下使用的注解,此处就不再累述。

七、读取配置文件

# 7.1 属性装配

有两种方式:使用 @Value 注解和 Environment 对象。

在 application.properties 中添加:

 
  1. ds.userName=root
  2. ds.password=tiger
  3. ds.url=jdbc:mysql://localhost:3306/test
  4. ds.driverClassName=com.mysql.jdbc.Driver

以上是自定义的配置。

创建一个配置类,如下:

 
  1. @Configuration
  2. public class WebConfig {
  3.  
  4. @Value("${ds.userName}")
  5. private String userName;
  6.  
  7. @Autowired
  8. private Environment environment;
  9.  
  10. public void show() {
  11. System.out.println("ds.userName:" + this.userName);
  12. System.out.println("ds.password:" + this.environment.getProperty("ds.password"));
  13. }
  14. }

通过 @Value 获取 config.userName 配置;通过 environment 获取 config.password 配置。

测试:

 
  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3.  
  4. public static void main(String[] args) {
  5. ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
  6. context.getBean(WebConfig.class).show();
  7. }
  8. }

打印结果:

 
  1. userName:root
  2. password:tiger

# 7.2 对象装配

创建一个封装类:

 
  1. @Component
  2. @ConfigurationProperties(prefix="ds")
  3. public class DataSourceProperties {
  4.  
  5. private String url;
  6.  
  7. private String driverClassName;
  8.  
  9. private String userName;
  10.  
  11. private String password;
  12.  
  13.  
  14. public void show() {
  15. System.out.println("ds.url=" + this.url);
  16. System.out.println("ds.driverClassName=" + this.driverClassName);
  17. System.out.println("ds.userName=" + this.userName);
  18. System.out.println("ds.password=" +this.password);
  19. }
  20.  
  21. }

此处省略 setter 和 getter 方法。

测试:

 
  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3.  
  4. public static void main(String[] args) {
  5. ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
  6. context.getBean(DataSourceProperties.class).show();
  7. }
  8. }

打印结果:

 
  1. ds.url=jdbc:mysql://localhost:3306/test
  2. ds.driverClassName=com.mysql.jdbc.Driver
  3. ds.userName=root
  4. ds.password=tiger

八、自动配置

在上文的例子中,我们其实就使用到自动配置了,在此小结中再举例说明,加深印象。

现有 2 个项目,一个 Maven 项目 和 Spring Boot 项目。

Spring Boot 项目引入 Maven 项目并使用 Maven 项目中写好的类。

# 8.1 编码

Maven 项目中的代码:

 
  1. public class Cache {
  2.  
  3. private Map<String,Object> map = new HashMap<String,Object>();
  4.  
  5. public void set(String key,String value) {
  6. this.map.put(key,value);
  7. }
  8.  
  9. public Object get(String key) {
  10. return this.map.get(key);
  11. }
  12. }
  13.  
  14. @Configuration
  15. public class CacheConfirguration {
  16.  
  17. @Bean
  18. public Cache createCacheObj() {
  19. return new Cache();
  20. }
  21. }

Spring Boot 项目引入 Maven 项目:

pom.xml 文件:

 
  1. <dependency>
  2. <groupId>com.light</groupId>
  3. <artifactId>cache</artifactId>
  4. <version>0.0.1-SNAPSHOT</version>
  5. </dependency>

测试:

 
  1. @SpringBootApplication
  2. public class SpringbootApplication extends SpringBootServletInitializer {
  3.  
  4. @Override
  5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  6. return application.sources(SpringbootApplication.class);
  7. }
  8.  
  9. public static void main(String[] args) {
  10. ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
  11.  
  12. CacheConfirguration conf = context.getBean(CacheConfirguration.class);
  13. System.out.println(conf);
  14.  
  15. Cache Cache = context.getBean(Cache.class);
  16. System.out.println(Cache);
  17. }
  18. }

打印结果:

 
  1. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.light.cache.Cache' available

从结果我们可知 Spring Boot 并不会自动配置第三方 jar 资源文件。

因为 @SpringBootApplication 注解包含 @ComponentScan 注解,项目启动时 Spring 只扫描与 SpringbootApplication 类同目录和子目录下的类文件,引入第三方 jar 文件无法被扫描,因此不能被 Spring 容器管理。

# 8.2 解决方案

方式一:

在启动类 SpringbootApplication 上添加 @Import(CacheConfirguration.class)。

方式二:

在 Maven 项目的 src/main/resources 目录下创建 META-INF 文件夹,在该文件夹下再创建 spring.factories 文件,内容如下:

 
  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  2. com.light.cache.CacheConfirguration

启动项目,结果如下:

九、条件配置

需要装配的类:

 
  1. public interface EncodingConvertor {
  2.  
  3. }
  4.  
  5. public class UTF8EncodingConvertor implements EncodingConvertor {
  6.  
  7. }
  8.  
  9. public class GBKEncodingConvertor implements EncodingConvertor {
  10.  
  11. }

配置类:

 
  1. @Configuration
  2. public class EncodingConvertorConfiguration {
  3.  
  4. @Bean
  5. public EncodingConvertor createUTF8EncodingConvertor() {
  6. return new UTF8EncodingConvertor();
  7. }
  8.  
  9. @Bean
  10. public EncodingConvertor createGBKEncodingConvertor() {
  11. return new GBKEncodingConvertor();
  12. }
  13. }

测试:

 
  1. @SpringBootApplication
  2. public class SpringbootApplication extends SpringBootServletInitializer {
  3.  
  4. @Override
  5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  6. return application.sources(SpringbootApplication.class);
  7. }
  8.  
  9. public static void main(String[] args) {
  10. ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
  11. Map<String, EncodingConvertor> map = context.getBeansOfType(EncodingConvertor.class);
  12. System.out.println(map);
  13. }
  14. }

打印结果:

 
  1. {createUTF8EncodingConvertor=com.light.springboot.config.UTF8EncodingConvertor@4c889f9d, createGBKEncodingConvertor=com.light.springboot.config.GBKEncodingConvertor@26f067d9}

从结果看出,Spring 帮我们装配了 2 个 Bean。

当我们需要根据系统环境的字符集选择性的装配其中一个 Bean 时,需要结合 @Conditional 注解 和 Condition 接口使用。如下:

创建条件类:

 
  1. public class UTF8Condition implements Condition {
  2.  
  3. @Override
  4. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  5. String encoding = System.getProperty("file.encoding");
  6. if (encoding != null) {
  7. return "utf-8".equals(encoding.toLowerCase());
  8. }
  9. return false;
  10. }
  11.  
  12. }
  13.  
  14. public class GBKCondition implements Condition {
  15.  
  16. @Override
  17. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  18. String encoding = System.getProperty("file.encoding");
  19. if (encoding != null) {
  20. return "gbk".equals(encoding.toLowerCase());
  21. }
  22. return false;
  23. }
  24.  
  25. }

Condition 用于判断是否进行装配,需要实现 matches 方法。当方法返回 true 时表示需要装配,否则反之。

修改配置类:

 
  1. @Configuration
  2. public class EncodingConvertorConfiguration {
  3.  
  4. @Bean
  5. @Conditional(UTF8Condition.class)
  6. public EncodingConvertor createUTF8EncodingConvertor() {
  7. return new UTF8EncodingConvertor();
  8. }
  9.  
  10. @Bean
  11. @Conditional(GBKCondition.class)
  12. public EncodingConvertor createGBKEncodingConvertor() {
  13. return new GBKEncodingConvertor();
  14. }
  15. }

在对应的 Bean 上添加 @Conditional 注解。

测试:

 
  1. @SpringBootApplication
  2. public class SpringbootApplication extends SpringBootServletInitializer {
  3.  
  4. @Override
  5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  6. return application.sources(SpringbootApplication.class);
  7. }
  8.  
  9. public static void main(String[] args) {
  10. ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
  11. System.out.println(System.getProperty("file.encoding"));
  12. Map<String, EncodingConvertor> map = context.getBeansOfType(EncodingConvertor.class);
  13. System.out.println(map);
  14. }
  15. }

打印结果:

 
  1. UTF-8
  2. {createUTF8EncodingConvertor=com.light.springboot.config.UTF8EncodingConvertor@24701bc1}

除了 @Conditional 之外,Spring Boot 还提供了其他注解进行条件装配:

 
  1. @ConditionalOnBean # 当指定 Bean 存在时进行装配
  2. @ConditionalOnMissingBean # 当指定 Bean 不存在时进行装配
  3. @ConditionalOnClass # 当指定 Class 在 classpath 中时进行装配
  4. @ConditionalOnMissingClass # 当指定 Class 不在 classpath 中时进行装配
  5. @ConditionalOnExpression # 根据 SpEL 表达式进行装配
  6. @ConditionalOnJava # 根据 JVM 版本进行装配
  7. @ConditionalOnJndi # 根据 JNDI 进行装配
  8. @ConditionalOnWebApplication # 当上下文是 WebApplication 时进行装配
  9. @ConditionalOnNotWebApplication # 当上下文不是 WebApplication 时进行装配
  10. @ConditionalOnProperty # 当指定的属性名的值为指定的值时进行装配
  11. @ConditionalOnResource # 当指定的资源在 classpath 中时进行装配
  12. @ConditionalOnCloudPlatform #
  13. @ConditionalOnSingleCandidate #

十、打包运行

打包的形式有两种:jar 和 war。

# 10.1 打包成可执行的 jar 包

默认情况下,通过 maven 执行 package 命令后,会生成 jar 包,且该 jar 包会内置了 tomcat 容器,因此我们可以通过 java -jar 就可以运行项目,如下图:

# 10.2 打包成部署的 war 包

让 SpringbootApplication 类继承 SpringBootServletInitializer 并重写 configure 方法,如下:

 
  1. @SpringBootApplication
  2. public class SpringbootApplication extends SpringBootServletInitializer {
  3.  
  4. @Override
  5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  6. return application.sources(SpringbootApplication.class);
  7. }
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run(SpringbootApplication.class, args);
  11. }
  12. }

修改 pom.xml 文件,将 jar 改成 war,如下:

 
  1. <packaging>war</packaging>

移除内置 tomcat:

 
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <!-- Servlet API -->
  12. <dependency>
  13. <groupId>javax.servlet</groupId>
  14. <artifactId>javax.servlet-api</artifactId>
  15. <version>3.1.0</version>
  16. <scope>provided</scope>
  17. </dependency>

打包成功后,将 war 包部署到 tomcat 容器中运行即可。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值