spring-boot-starter-parent在pom文件中具体是做什么的?

17

我正在开发一个项目,它不是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 Web服务器上运行,对我来说有点奇怪的是,当我将这段代码片段添加到我的pom中时,index.jsp会在浏览器中准确地显示,但当我删除它时,它会给出404未找到控制器的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>
4个回答

7

Spring Boot提供了许多“Starter”(起动器),可以让您将jar包添加到类路径。例如:spring-boot-starter-security,spring-boot-starter-web等。 "spring-boot-starter-parent"是一个特殊的起动器,它提供了有用的Maven默认值,即自动添加所有必需的jar包和其他内容。它还提供了一个依赖管理部分,使您可以在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错误。为什么会这样? - ShakibaZar

5

编辑:请参考官方文档

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

它为子POM提供了通用配置的地方,例如:

DependenciesProperties

例如: 这里是spring-boot-starter-parent的父级POM配置1.4.2.RELEASE spring-boot-dependencies

<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>
子POM的常见属性
<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>

常见的依赖项适用于子级。

1
如果您能提供更多关于您的pom完整性的信息,我们可以进一步了解并为您提供更多帮助。
另一方面,父pom spring-boot-starter-parent包含了完整的依赖项(mvc、cache、jpa)和公共属性,以保持依赖项版本的良好一致性,可用于您的项目或子模块。
基本上,您可以根据需要在您的pom.xml中添加一些启动器(web、jpa、batch等)。对于您的示例,您只需将一个mvc启动器添加到您的pom.xml中,而无需添加其他依赖项,这样您的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>

0

spring-boot-starter-parent 的主要特点是为我提供了兼容版本的依赖项。

没有完整的 pom,很难说会发生什么,但我可以猜测您的 pom 中存在一些依赖冲突,并通过添加 spring-boot-starter-parent 来解决它。


1
您的回答可以通过添加更多支持性信息来改进。请[编辑]以添加更多细节,例如引文或文档,以便他人确认您的答案正确无误。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接