zoukankan      html  css  js  c++  java
  • (003)Spring Boot之两种引入jar包的方式

      1、继承父工程,用parent标签,如下:

      pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.edu.spring</groupId>
        <artifactId>springboot</artifactId>
        <version>1.0.0</version>
        <packaging>jar</packaging>
    
        <name>springboot</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
        
        <parent> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-parent</artifactId> 
            <version>2.0.4.RELEASE</version> 
            <relativePath/> 
        </parent> 
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    View Code

      App.java

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class App 
    {
        @Bean
        Runnable createRunnable(){
            return () -> {System.out.println("spring boot is run");};
        }
        
        public static void main( String[] args )
        {
            
            ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
            context.getBean(Runnable.class).run();
        }
    }
    View Code

      运行结果如下:

       2、使用dependencyManagement标签,如下:

       pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.edu.spring</groupId>
        <artifactId>springboot</artifactId>
        <version>1.0.0</version>
        <packaging>jar</packaging>
    
        <name>springboot</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
        
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.1.6.RELEASE</version>
                    <scope>import</scope>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    View Code

      运行App.java,结果如下:

       可以看到,两种方式,都可以正常运行。

  • 相关阅读:
    svn 回退/更新/取消至某个版本命令详解
    SVN版本回退
    用Visual Studio编辑Linux代码
    vim——打开多个文件、同时显示多个文件、在文件之间切换
    vim下的ctags和taglist等的使用和配置
    Uber优步北京第二、三组奖励政策
    Uber优步北京第一组奖励政策
    uber在限制新司机加入了,看看新政策把
    软件架构 "4+1" 视图模型
    软件体系结构经典问题——KWIC的分析和解决
  • 原文地址:https://www.cnblogs.com/javasl/p/11827822.html
Copyright © 2011-2022 走看看