4.21 spring boot 与 gradle

纯spring项目

chapter04/boot-simple

目录结构

子项目builde.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/libs-snapshot" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "https://repo.spring.io/libs-milestone" }
    }

    dependencies {
        classpath boot.springBootPlugin
    }
}

apply plugin: 'org.springframework.boot'

dependencies {
    compile boot.starter
}

jar {
    manifest {
        attributes("Created-By"         : "Iuliana Cosmina",
                    "Specification-Title": "Pro Spring 5",
                    "Main-Class"         : "com.apress.prospring5.ch4.Application",
                    "Class-Path"         : configurations.compile.collect { it.getName() }.join(' '))
    }
}

boot.springBootPlugin行引用了父项目的 build.gradle文件中定义的 boot数组的 springBootPlugin属性。该文件的 内容片段(仅与 SpringBoot相关的配置)如下所示:

ext{
...
bootVersion = '2.0.6.RELEASE'
...
boot = [
springBootPlugin: "org.springframework.boot:spring-boot-gradle-plugin:$bootVersion",
starter:"org.springframework.boot:spring-boot-starter:$bootVersion",
...     
}
package com.apress.prospring5.ch4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by iuliana.cosmina on 3/19/17.
 */
@Component
public class HelloWorld {

    private static Logger logger = LoggerFactory.getLogger(HelloWorld.class);

    public void sayHi() {
        logger.info("Hello World!");
    }
}
package com.apress.prospring5.ch4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Arrays;

/**
 * Created by iuliana.cosmina on 3/19/17.
 */
@SpringBootApplication
public class Application {

    private static Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String... args) throws Exception {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
        assert (ctx != null);
        logger.info("The beans you were looking for:");

        // listing all bean definition  names
        Arrays.stream(ctx.getBeanDefinitionNames()).forEach(logger::info);

        HelloWorld hw = ctx.getBean(HelloWorld.class);
        hw.sayHi();

        System.in.read();
        ctx.close();
    }
}

spring web项目

chapter04/boot-web

文件结构

子项目build.gradle目录

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/libs-snapshot" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "https://repo.spring.io/libs-milestone" }
    }

    dependencies {
        classpath boot.springBootPlugin
    }
}

apply plugin: 'org.springframework.boot'

dependencies {
    compile boot.starterWeb
}

jar {
    manifest {
        attributes("Created-By"         : "Iuliana Cosmina",
                    "Specification-Title": "Pro Spring 5",
                    "Main-Class"         : "com.apress.prospring5.ch4.WebApplication",
                    "Class-Path"         : configurations.compile.collect { it.getName() }.join(' '))
    }
}
package com.apress.prospring5.ch4.ctrl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by iuliana.cosmina on 3/19/17.
 */
@RestController
public class HelloWorld {

    @RequestMapping("/")
    public String sayHi() {
        return "Hello World!";
    }
}
package com.apress.prospring5.ch4;

import com.apress.prospring5.ch4.ctrl.HelloWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Arrays;

/**
 * Created by iuliana.cosmina on 3/19/17.
 */
@SpringBootApplication(scanBasePackageClasses = HelloWorld.class)
public class WebApplication {

    private static Logger logger = LoggerFactory.getLogger(WebApplication.class);

    public static void main(String... args) throws Exception {
        ConfigurableApplicationContext ctx = SpringApplication.run(WebApplication.class, args);
        assert (ctx != null);
        logger.info("Application started...");

        System.in.read();
        ctx.close();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容