0

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>
  • You need to configure the [Lombok compiler plugin](https://projectlombok.org/setup/maven) in that POM file too – Jorn Aug 21 '23 at 14:32
  • I have lombok dependency and plugin in pom.xml, but it doesn't work – Yana Aziamblouskaya Aug 21 '23 at 16:18
  • Does this answer your question? [MapStruct and Lombok not working together](https://stackoverflow.com/questions/47676369/mapstruct-and-lombok-not-working-together) – Jan Rieke Aug 22 '23 at 05:58

1 Answers1

0

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.