簡體   English   中英

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

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

我正在開發一個不是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 webserver上運行,對我來說很奇怪的是,當我將這段代碼添加到我的pom時,index.jsp將完全出現在瀏覽器中但當我刪除它時,它為我的控制器提供404未找到的URL。 為什么即使我的項目不是春季啟動項目需要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>

CHILD POM常見配置提供了場所 例如

DependenciesProperties

eg這里是父POM配置1.4.2.RELEASE spring-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 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>

孩子的共同依賴

如果您可以提供更多信息,例如您的pom完整性,我們可以進一步了解並幫助您。

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

基本上,您可以根據需要在web上添加一些啟動器(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>

Spring Boot提供了許多“Starters”,可以將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”默認添加了很多東西,所以我們不必擔心這些事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM