天天看点

maven 中<dependency>标签中的 scope属性的作用maven scope属性值设置含义

maven scope属性值设置含义

1、枚举各个属性值的含义

  • compile,缺省值,适用于所有阶段,会打包进项目。
  • provided,类似compile,期望JDK、容器或使用者会提供这个依赖。
  • runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
  • test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
  • system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。

2、其它类型的属性值都比较容易理解,这里重点比较一下compile和runtime之间的区别:

(1)先描述一个简单的例子:模块A依赖X,此时X的scope设置的值为runtime;

(2)另一模块B依赖A,则B在编译时不会依赖X(编译时不会有任何问题);

如果原先X的scope设置为compile,则说明在编译的时B需要显示的调用X的相关类,在maven依赖中最常见的设置为runtime的依赖是JDBC,主要原因是由于jdbc中对驱动类的配置是采用反射的机制在配置文件中配置了class-name;

maven 中<dependency>标签中的 scope属性的作用maven scope属性值设置含义

解决了当

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

时候

出现问题

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

[main 2018-11-24 14:56:56 ERROR]-org.springframework.boot.SpringApplication Application startup failed

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)

    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)

    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)

    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)

    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

    at com.yonyou.yuncai.angang.project.service.ProjectServiceApplication.main(ProjectServiceApplication.java:18)

Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:189)

    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:162)

    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134)

    ... 8 common frames omitted

maven 中<dependency>标签中的 scope属性的作用maven scope属性值设置含义
maven 中<dependency>标签中的 scope属性的作用maven scope属性值设置含义

换成这个解决上述问题

继续阅读