. Advertisement .
..3..
. Advertisement .
..4..
“UnsatisfiedDependencyException- unsatisfied dependency expressed through field” is a common that many programmers encounter. It happens in many ways. What is the cause and how to fix it? In this blog, we will give you some useful information to help you fix this issue. Read on.
How does the error “UnsatisfiedDependencyException- unsatisfied dependency expressed through field” happen?
When you run your program, you can easily encounter the following error:
UnsatisfiedDependencyException- unsatisfied dependency expressed through field
When a Spring application tries to wire a bean but is unable to resolve one of the necessary dependencies, so an error like above occurs.
Another factor contributes to this error. If a bean that hasn’t been loaded in the spring boot application is auto-wired into the constructor parameter. The bean must be loaded in the spring boot application context if it is auto-wired into another bean. If not, the bean won’t be automatically wired for the bean. For a variety of reasons, including the bean not being available, loading was unsuccessful, an exception occurred while loading, an exception occurred during initialization, etc., the bean is not loaded in the context. The nested exception will display the exception’s cause. org.springframework.beans.factory. Error generating bean with name defined in file: UnsatisfiedDependencyException.
What is a bean in Spring?
Beans are the main components of your application in Spring and are controlled by the Spring IoC container. It is an object that a Spring IoC container instantiates, generally manages and assembles. Otherwise, a bean is just another one of your application’s many objects. Beans with their interdependencies are reflected in a container’s config metadata.
How to fix the error “UnsatisfiedDependencyException- unsatisfied dependency expressed through field”?
Solution 1: Set up the package scan on the parent package
To fix the error “unsatisfiedDependencyException- unsatisfied dependency expressed through field”, you can set up the package scan on the parent package to make sure all pertinent classes are present as the following:
@SpringBootApplication
@ComponentScan(basePackages = {"com.baeldung.dependency.exception"})
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}
After doing that, your error will be completely resolved.
Solution 2: The @Repository tag should be added to the ClientRepository
The @Repository tag should be added to the ClientRepository. Your current setup prevents Spring from scanning the class and learning about it. The ClientRepository class will not be found at the time of booting and wiring. If including the @Repository tag does not solve the issue, we believe that the ClientService and ClientServiceImpl may be the cause. Try adding a @Service annotation to the ClientService (interface). You don’t have to supply a name with the optional argument @Service because your service should only have one implementation (“clientService”). Using the name of the interface, Spring will automatically generate it.
Additionally, as Bruno pointed out, the ClientController does not require the @Qualifier because there is only one implementation of the service.
ClientService.java
@Service
public interface ClientService {
void addClient(Client client);
}
ClientServiceImpl.java (option 1)
@Service
public class ClientServiceImpl implements ClientService{
private ClientRepository clientRepository;
@Autowired
public void setClientRepository(ClientRepository clientRepository){
this.clientRepository=clientRepository;
}
@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}
ClientServiceImpl.java (option 2/preferred)
@Service
public class ClientServiceImpl implements ClientService{
@Autowired
private ClientRepository clientRepository;
@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}
ClientController.java
@Controller
public class ClientController {
private ClientService clientService;
@Autowired
//@Qualifier("clientService")
public void setClientService(ClientService clientService){
this.clientService=clientService;
}
@RequestMapping(value = "registration", method = RequestMethod.GET)
public String reg(Model model){
model.addAttribute("client", new Client());
return "registration";
}
@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
this.clientService.addClient(client);
return "home";
}
}
Solution 3: Use @Qualifier
To distinguish between the repositories, use @Qualifier. This method also helps you resolve your problem. Look at the following examples:
@Qualifier("dresses")
@Repository
public class DressRepository implements InventoryRepository {
}
@Qualifier("shoes")
@Repository
public class ShoeRepository implements InventoryRepository {
}
You must also include the following qualification in the PurchaseDeptService constructor dependency:
public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) {
this.repository = repository;
}
By this, Spring will make DressRepository as the sole practical choice into PurchaseDeptService.
Conclusion
We hope that you will enjoy our article about the error. With this knowledge, we know that you can fix your error “unsatisfiedDependencyException- unsatisfied dependency expressed through field” quickly by following these steps! If you still have any other questions about fixing this syntax error, please leave a comment below. Thank you for reading!
Read more
Leave a comment