java spring-boot-starter-parent 在 pom 文件中究竟做了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43305016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:19:19  来源:igfitidea点击:

What does spring-boot-starter-parent exactly do in pom file?

javaspringmavenspring-mvcspring-boot

提问by ShakibaZar

I'm developing a project which is not Spring boot but also spring mvc. I mean I don't have this class for example in my project:

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

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

I have these three classes for configuration file of spring mvc:

我有这三个类用于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;
    }

}

Second:

第二:

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()");
}}

Third,

第三,

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!");
    }
}

There is a controller and jsp file and I've configured it to run on tomcat webserver, something is strange for me is that when I add this snippet of code to my pom, index.jsp will appear in browser exactly but when I remove it , it gives 404 not found url for my controller. why is it that even my project is not a spring boot project need spring boot starter parent? I thought below code is related to spring boot and since my project is not spring boot but spring mvc, doesn't need it. but it has problem without this code added in pom:

有一个控制器和 jsp 文件,我已经将它配置为在 tomcat 网络服务器上运行,对我来说有些奇怪的是,当我将这段代码添加到我的 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>

回答by Mr.Arjun

It provides place for common configurationfor CHILD POMs. for e.g.

CHILD POMs 的通用配置提供了位置。例如

Dependencies& Properties

Dependencies& Properties

For e.g.Here is the parent POM configuration 1.4.2.RELEASEspring-boot-dependencieswhich is parent of spring-boot-starter-parent

因为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>

common properties for child POMs

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>

common dependencies for childs

孩子的共同依赖

回答by Mr.Arjun

Spring Boot provides a number of “Starters” that let you add jars to your classpath. For ex. spring-boot-starter-security,spring-boot-starter-web etc. The "spring-boot-starter-parent" is a special starter that provides useful Maven defaults i.e it adds all the required jars and other things automatically. It also provides a dependency-management section so that you can omit version tags for dependencies you are using in pom.xml. For ex. suppose you want to create web application with spring boot so you need to add the following.

Spring Boot 提供了许多“Starters”,可让您将 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>

Now note here that tag is omitted. So ultimately the "spring-boot-starter-parent" adds so many things by default so we no need to worry about those things.

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

回答by Hatim

If you can provide more informations, like your pom integrality, we can look futher to inderstand and help you more.

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

In the other hand, the parent pom spring-boot-starter-parent, contains the full dependencies(mvc, cache, jpa) and commons properties to maintain dependencies versions in a good consistency, to be used in your project or/and submodules.

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

Basically, you can add some starters in your pom.xml depending on your need (web, jpa, batch....) For your example, you can just add a starter mvc to your pom.xml without adding other dependencies, so your pom.xml can just be like that :

基本上,您可以根据需要在 pom.xml 中添加一些启动器(web、jpa、批处理....)对于您的示例,您可以在 pom.xml 中添加一个启动器 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>