Spring Cloud 之 Lombok集成(四)

Lombok本身和SpringCloud毫无关系,把它单独拿出来讲主要是因为后续会在其它模块用到Lombok。当然网上对于Lombok的说法也众说纷纭,有好有坏,其实大可不必,只要结合自身情况,决定要不要使用就行了,这里不展开讨论。

下面开始Lombok之美吧!!!

 

1、在根项目的build.gradle中加入Lombok依赖

subprojects中标红的两行
buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
        springBootManagementVersion = '1.0.8.RELEASE'
        springCloudVersion = 'Finchley.RC1'
    }
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        mavenCentral()
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("io.spring.gradle:dependency-management-plugin:${springBootManagementVersion}")
    }
}

version = '0.0.1-SNAPSHOT'

subprojects {
    apply plugin: 'java'
    apply plugin: 'application'
    apply plugin: 'idea'
    apply plugin: 'eclipse'
    apply plugin: 'war'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
    jar {
        enabled = true
    }
    bootJar {
        classifier = 'boot'
    }
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        mavenCentral()
        maven { url 'https://repo.spring.io/snapshot' }
        maven { url 'https://repo.spring.io/milestone' }
    }
    dependencies {
        compile(
                'org.springframework.boot:spring-boot-starter-web',
                'org.springframework.boot:spring-boot-starter-tomcat',
                'org.springframework.boot:spring-boot-starter-actuator',
                'org.springframework.cloud:spring-cloud-starter',
                'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client',
                'com.google.guava:guava:23.0',
                "org.projectlombok:lombok"
        )
        testCompile(
                "org.springframework.boot:spring-boot-starter-test",
                "org.springframework:spring-test",
                "junit:junit:4.12",
                "org.projectlombok:lombok"
        )
    }
    dependencyManagement {
        imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
        imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" }
    }

}

 

2、修改代码

第3行增加Slf4j注解

第8行打印日志

 1 @RestController
 2 @RequestMapping("demo")
 3 @Slf4j
 4 public class DemoController {
 5 
 6     @RequestMapping("service")
 7     public String service() {
 8         log.info("谁在撩我?");
 9         return "x-demo-service hello.";
10     }
11 }

 

3、Lombok生效配置,有时Lombok不生效,编译不通过,需要手动改一下idea的配置,如下图将Enable annotation processing前面选中。

 

4、重启x-demo-service服务

浏览器访问http://localhost:8081/demo/service接口,可以在idea console看到如下输出,说明lombok集成成功。

posted @ 2021-02-22 17:09  shileishmily  阅读(445)  评论(0编辑  收藏  举报