【问题标题】:What does spring-boot-starter-parent exactly do in pom file?spring-boot-starter-parent 在 pom 文件中到底做了什么?
【发布时间】:2017-09-04 10:13:09
【问题描述】:

我正在开发一个项目,它不是 Spring boot 而是 spring mvc。我的意思是我的项目中没有这个类:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

spring mvc的配置文件我有这三个类:

@Import(WebSocketConfig.class)
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "......")

public class MainConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/Content/**")
                .addResourceLocations("/Content/");
        registry.addResourceHandler("/Scripts/**")
                .addResourceLocations("/Scripts/");


    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver
                = new InternalResourceViewResolver();
        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

第二:

public class MainInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    public static HashMap<String, String> response_code = new HashMap<String, String>();


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { MainConfiguration.class,
        WebSocketConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        Security.addProvider(new BouncyCastleProvider());
        servletContext.addListener(new MainContextListener());
        System.out.println("MainInitializer.onStartup()");
}}

第三,

public class MainContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("Context Initialized");
        Security.addProvider(new BouncyCastleProvider());
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("Shutting down!");
    }
}

有一个控制器和 jsp 文件,我已经将它配置为在 tomcat 网络服务器上运行,对我来说奇怪的是,当我将这段 sn-p 代码添加到我的 pom 时,index.jsp 将准确地出现在浏览器中但是当我删除它时,它会为我的控制器提供 404 not found url。为什么连我的项目都不是spring boot项目需要spring boot starter parent?我认为下面的代码与spring boot有关,因为我的项目不是spring boot而是spring mvc,所以不需要它。但是如果没有在 pom 中添加此代码就会出现问题:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

【问题讨论】:

    标签: java spring maven spring-mvc spring-boot


    【解决方案1】:

    编辑:另请参阅官方文档

    https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application.dependencies

    它为CHILD POMs 提供了通用配置的地方。 例如

    Dependencies & Properties

    对于e.g. 这是父POM配置1.4.2.RELEASEspring-boot-dependencies 这是spring-boot-starter-parent的父级​​

    <properties>
        <activemq.version>5.13.4</activemq.version>
        <antlr2.version>2.7.7</antlr2.version>
        <appengine.version>1.9.44</appengine.version>
        <artemis.version>1.3.0</artemis.version>
        <aspectj.version>1.8.9</aspectj.version>
        <assertj.version>2.5.0</assertj.version>
        <atomikos.version>3.9.3</atomikos.version>
        <bitronix.version>2.1.4</bitronix.version>
        <caffeine.version>2.3.4</caffeine.version>
    

    child POMs 的常用属性

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>1.4.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <type>test-jar</type>
                <version>1.4.2.RELEASE</version>
            </dependency>
            <dependency>
    

    子类的常见依赖项

    【讨论】:

      【解决方案2】:

      Spring Boot 提供了许多“启动器”,可让您将 jars 添加到您的 类路径。例如。 spring-boot-starter-security、spring-boot-starter-web 等。 “spring-boot-starter-parent”是一个特殊的启动器,它提供了有用的 Maven 默认,即它添加了所有必需的 jars 和其他东西 自动地。它还提供了一个依赖管理部分,以便您 可以省略您在 pom.xml 中使用的依赖项的版本标签。例如。 假设您想使用 Spring Boot 创建 Web 应用程序,因此您需要 添加以下内容。

      <dependencies>
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      </dependencies>
      

      现在请注意这里省略了标记。所以最终 “spring-boot-starter-parent”默认添加了很多东西,所以我们不需要担心这些东西。

      【讨论】:

      • 我的问题是我的项目不是 Spring Boot 项目。但是当我从依赖项中删除 spring-boot-starter-parent 时,index.jsp 给出 404 错误。为什么会这样?
      【解决方案3】:

      如果您可以提供更多信息,例如您的 pom 完整性,我们可以进一步了解并为您提供更多帮助。

      另一方面,父 pom spring-boot-starter-parent 包含完整的依赖项(mvc、cache、jpa)和 commons 属性,以保持依赖项版本的良好一致性,以便在您的项目中使用或/和子模块。

      基本上,您可以根据需要在 pom.xml 中添加一些启动器(web、jpa、批处理....) 对于您的示例,您可以在 pom.xml 中添加一个 starter mvc 而无需添加其他依赖项,因此您的 pom.xml 可以是这样的:

       <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>1.5.9.RELEASE</version>
      </parent>
      
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
      
      <properties>
          <java.version>1.8</java.version>
      </properties>
      
      
      <build>
          <plugins>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
              </plugin>
          </plugins>
      </build>
      

      【讨论】:

        猜你喜欢
        • 2022-07-30
        • 2020-05-23
        • 2021-09-20
        • 2018-05-20
        • 2018-07-18
        • 2015-03-10
        • 1970-01-01
        • 2022-12-15
        • 1970-01-01
        相关资源
        最近更新 更多