Home > OS >  Field userRepository in com.x.x.service.UserServiceImpl required a bean of type 'com.x.x.reposi
Field userRepository in com.x.x.service.UserServiceImpl required a bean of type 'com.x.x.reposi

Time:05-12

Good morning ! despite all my research I can not solve this problem with application spring someone can help me I use jdk 17;

My Service class

@Service
public class UserServiceImpl implements UserService{

@Autowired
public UserRepository userRepository;

@Override
public void save(UserModel user) {
    userRepository.save(user);
}
@Override
public void update(UserModel user) {

}
@Override
public UserModel load(UserModel user) {
    return null;
}

@Override
public List<UserModel> loadByMultiple(String data) {
    return null;//userRepository.loadByMultiple(data);
}


@Override
public UserModel loadByUsername(String username) {
    return null;
}

@Override
public UserModel loadByEmail(String email) {
    return null;
}

@Override
public UserModel loadByPhone(String phone) {
    return null;
}

@Override
public void delete(UserModel user) {

}
}

My Repository class

@Repository
public interface UserRepository extends JpaRepository<UserModel, Long> {

/*@Query("""
        SELECT u FROM user u WHERE u.user_name LIKE % :data% or  u.last_name = LIKE % :data%
         """)
List<UserModel> loadByMultiple(@Param("data") String data);*/

}

@SpringBootApplication
public class DecouverteBackendApplication {

public static void main(String[] args) {
    SpringApplication.run(DecouverteBackendApplication.class, args);
}

}

<?xml version="1.0" encoding="UTF-8"?>

4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.7 com.infinitylogic Decouverte_backend 0.0.1-SNAPSHOT Decouverte_backend Decouverte_backend <java.version>17</java.version> org.hibernate hibernate-core 5.2.1.Final org.hibernate hibernate-entitymanager 5.2.3.Final

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</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-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </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>
</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>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>
    </plugins>
</build>

My project structure enter image description here

CodePudding user response:

When the repository package is different to @SpringBootApplication/@EnableAutoConfiguration, base package of @EnableJpaRepositories is required to be defined explicitly.

Try to add @EnableJpaRepositories("your.package.structure.here") to DecouverteBackendApplication

CodePudding user response:

Try this (using Lombok's RequiredArgsConstructor):

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService{

private final UserRepository userRepository;

@Override
public void save(UserModel user) {
    userRepository.save(user);
}
...
  • Related