Error when using the @Slf4j annotation "log" using the Spring Boot tutorial

Asked

Viewed 309 times

1

I am studying creating REST Apis with Spring Boot following the following tutorial: Building REST services with Spring.

However when implementing to the class Loaddatabase from the tutorial I’m getting a build error while updating the attribute log, even with the method being annotated @Slf4j.

NOTE: Project created on Spring Iniializr, code written using Eclipse, and executed with Maven per command line using ./mvnw spring-boot:run.

This is the class Loaddatabase

package com.ricardo.studySpring.loaders;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ricardo.studySpring.entities.Product;
import com.ricardo.studySpring.repotories.ProductRepository;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class LoadDatabase {

    @Bean
     public CommandLineRunner initDatabase(ProductRepository repository) {
        return args -> {
                log.info("Preloading " + repository.save(new Product("Banan", 2.25)));
                log.info("Preloading " + repository.save(new Product("carne", 20.0)));
        };
    }
}

This is the file 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ricardo</groupId>
    <artifactId>studySpring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>studySpring</name>
    <description>Study of Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is the output log Maven

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.724 s
[INFO] Finished at: 2019-10-02T21:01:14-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.8.RELEASE:run (default-cli) on project studySpring: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'loadDatabase' defined in file [/home/ricardo/workspaces/projws-backend/studySpring/target/classes/com/ricardo/studySpring/loaders/LoadDatabase.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ricardo.studySpring.loaders.LoadDatabase$$EnhancerBySpringCGLIB$$42d56eee]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: 
[ERROR]     log cannot be resolved
[ERROR]     log cannot be resolved
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

  • 1

    You installed Lombok in your IDE?

  • Well, no. I was using IDE only as a text editor and sometimes I use Vim. I am compiling and running using Maven with the MVNW file that comes in the project generated by start.spring.io. I installed and worked, even when running using the command ./mvnw spring-boot:run , but only when the code is written using the IDE with the plugin. The strange thing is that there was no problem with the annotation @Data when writing code without installing. I will update the question. You can write the answer for me to dial, but if possible it would be nice to explain why this happens.

  • In the meantime you haven’t made any dependency changes (explicitly log dependency for example)?

  • No, it’s all the same as the tutorial. The only thing I tried after you wrote the first comment was to add <scope>provided</scope> on the Lombok dependency of pom.xml, but it made no difference. I’ve been researching Lombok in the meantime, everywhere they have Ombok installed in an IDE, I’m just not sure if it’s just so the IDE’s auto-broker can work properly or if the build tool (in my case, Maven) needs it to be installing in an IDE to work properly.

  • However, I think this is outside the scope of the question. You can write the answer and I will mark it as accepted. If you can explain why this is happening, it will be great, it will make the answer richer. Otherwise no problem, still solves the issue and later I will open another question to specifically address the issue of the obligation to install Lombok in an IDE.

1 answer

1


As suggested in the comment, perform the installation of Lombok in your IDE should solve the problem.

But out of curiosity, to find out why performing this action actually solves the problem I took a look at how Lombok actually works under the Hood.

The processing of Lombok annotations is performed using the JSR 269: Pluggable Annotation Processing API.

When performing the installation of Lombok, the following path is included in the classpath: /META-INF/services/javax.annotation.processing.Processor. During compilation using the javac, the annotations are interpreted in accordance with.

2.7 Please Give a short Description of the underlying technology or Technologies: In order to realize the full Benefits of Annotations, a standardized Mechanism is necessary to process them in contexts other than a running jvm. Since Annotations are Added to declarations (classes, methods, Fields, etc.) , the API needs to include a model of program Structure. Additionally, the Processors for Different Annotations should be Able to work cooperatively and the set of Processors that are run should be Able to Depend on what Annotations are present in the code being processed.

2.8 Is there a proposed package name for the API Specification? (i.e., javapi.Something, org.Something, etc.) All the API Elements are expected to reside in the javax namespace; the language Modeling portions of the API Might be called javax.mirror.declaration, javax.mirror.type, etc. while the Processing Related portions of the API Might be called javax.annotation.Processing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.