Yana Aziamblouskaya
Yana Aziamblouskaya

Reputation: 161

variable not initialized in the default constructor Lombok Spring

There is an issue variable usersRepository not initialized in the default constructor called in this class

@Service
@RequiredArgsConstructor
public class UsersDetailsService implements UserDetailsService {

    private final UsersRepository usersRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Optional<User> user = usersRepository.findByEmail(username);

        if(user.isEmpty())
            throw new UsernameNotFoundException("User not found");

        return new UsersDetails(user.get());
    }
}

Also I have problems with all Lombok annotations during compilation my program. I think there is a problem with some versions. I have already installed and updated lombok plugin, but the problem still hasn't solved

There is my pom.xml file

<?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.7.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>CharityOrganizationSpringApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>CharityOrganizationSpringApp</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>17</java.version>
        <modelmapper.version>3.0.0</modelmapper.version>
        <java-jwt.version>4.4.0</java-jwt.version>
        <mapstruct.version>1.5.3.final</mapstruct.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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</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>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>${modelmapper.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>${java-jwt.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>16</source>
                    <target>16</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Upvotes: 1

Views: 4415

Answers (5)

Isidro.rn
Isidro.rn

Reputation: 314

If you are getting this error on IntelliJ please try this. I just ran into the same issue and found the problem was my annotation processor config.

wrong config

I don't know why that path was there or who added it but neither adding the proper path to the library helped.

Selecting "obtain processors from project classpath" fixed it.

fix

Upvotes: 0

Dasun wijesundara
Dasun wijesundara

Reputation: 696

For Java 23 here it can be fixed with,

  1. Having the latest maven-compiler-version (it's version 3.13.0 at the time of writing this article)
  2. Setup maven.compiler.proc property with full value.

https://dzone.com/articles/using-lombok-library-witk-jdk-23

Upvotes: 0

celikgo
celikgo

Reputation: 137

add those lines into build.gradle file. additionally Java 21 and lombok are compatible.

implementation 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

Upvotes: 3

gkephorus
gkephorus

Reputation: 1386

I bumped into this as well. I had to add the Maven artifact lombok-mapstruct-binding to the maven-compiler-plugin like so:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${compiler-plugin.version}</version>
    <configuration>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${org.mapstruct.version}</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </path>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-mapstruct-binding</artifactId>
                <version>0.2.0</version>
            </dependency>
        </annotationProcessorPaths>
        <compilerArgs>
            <arg>-parameters</arg>
        </compilerArgs>
    </configuration>
</plugin>

This solved the issue for me. I read in the changelog of lombok I should have done this ever since version v1.18.16 (January 28th, 2021). I was just lucky I guess.

Upvotes: 4

user22423960
user22423960

Reputation:

I'm not sure if your wording is slightly off here but when you say "variable usersRepository not initialized in the default constructor", this implies you're creating an instance of UsersDetailsService by calling an empty constructor (no arguments).

If this is the case then usersRepository will be null since it is only initialized in the constructor created from @RequiredArgsConstructor.

If you are expecting the constructor created from @RequiredArgsConstructor to be called automatically through dependency injection and usersRepository is still null then check that you are creating a UsersRepository bean that can be injected. Annotating UsersRepository with @Repository will accomplish this if you are using a JPA repository.

Also, if you are using a JPA repository then ensure you have the @EnableJpaRepositories annotation in your app config, that points to the base package of your repositories.

Upvotes: 0

Related Questions