close

[Solved] How to fix: Error creating bean with name : Unsatisfied dependency expressed through field

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error How to fix: Error creating bean with name : Unsatisfied dependency expressed through field in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to fix: Error creating bean with name : Unsatisfied dependency expressed through field Error Occurs?

Today I am facing the following error How to fix: Error creating bean with name : Unsatisfied dependency expressed through field in Java.

How To Solve fix: Error creating bean with name : Unsatisfied dependency expressed through field Error ?

  1. How To Solve fix: Error creating bean with name : Unsatisfied dependency expressed through field Error ?

    To Solve fix: Error creating bean with name : Unsatisfied dependency expressed through field Error First, you don't have an appropriate project packaging structure. Then, you don't have a base package.

  2. How to fix: Error creating bean with name : Unsatisfied dependency expressed through field

    To Solve fix: Error creating bean with name : Unsatisfied dependency expressed through field Error First, you don't have an appropriate project packaging structure. Then, you don't have a base package.

Solution 1

In your User class you declare id with int type

@Id
@Column(name = "id", unique = true)
private int id;

But in the repository interface, you declared Long

public interface UserRepository extends JpaRepository<User, Long> {}

So, in User class change type of id like,

@Id
@Column(name = "id", unique = true)
private Long id;

And avoid your new error, use @Service annotation at UserService interface like

@Service
public interface UserService {
    List<User> getUsers();
}

Solution 2

First, you don’t have an appropriate project packaging structure. Then, you don’t have a base package. Spring always mentions to have a base package for proper component scans. Without base package, it’s a bad practice.

The best practice should be like :

src
-main
--base package(x.x.x.) (for example : com.project)
---controllers
---models
---repositories
-resources
  1. Xml – Based config :<context:component-scan base-package="com.project" />
  2. Annotation or Java- based config :@ComponentScan("com.project")

Secondly, if you have these kind of package, on a contrary, you have to mention package names separately.

You have to do it like this :

  1. Xml – Based config :<context:component-scan base-package="main.controllers, main.repositories" />
  2. Annotation or Java- based config :@ComponentScan({"main.controllers", "main.repositories"})

Moreover, you are having a spring-boot project. So what you do is add @ComponentScan in the Application class :

@ComponentScan({"main.controllers", "main.repositories"})
@EnableJpaRepositories("main.repositories")
@SpringBootApplication
public class Application {
  public static void main(String[] args) {
     SpringApplication.run(Application.class, args);
  }
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read