S.P. S.P. - 27 days ago 13
Java Question

No qualifying bean found for dependency when create a new package for my class

I'm new in spring and I'm having some problems with my testing project. All is working fine, but I want to refactor my packages and change some of my class in another package to can organize better my packages.

This is my package that works:

src/main/java
- com.testing
- Application.java
- VehicleService.java
- IVehicleService.java
- VehicleResource.java


If i try to do this:

src/main/java
- com.testing
- Application.java
-service
- VehicleService.java
-Interfaces
- IVehicleService.java
-Resources
- VehicleResource.java


I get this error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [interfaces.IVehicleService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


VehicleService:

package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.org.testing.Vehicle;

import interfaces.IFileService;
import interfaces.IVehicleService;

@Component
public class VehicleService implements IVehicleService {

@Autowired
IFileService fileService;

@Transactional
public void addVehicle(Vehicle vehicle){
fileService.createVehicle(vehicle);
}
}


IVehicleService:

package interfaces;

import com.org.testing.Vehicle;

public interface IVehicleService {
void addVehicle(Vehicle vehicle);
}
}


VehicleResource:

package resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.org.testing.Vehicle;

import interfaces.IVehicleService;

@Component
@Path("/add")
public class VehicleResource {
private Vehicle vehicleNew = new Vehicle("T16 entity", "45678BBB", "Salva");
private final int ID = 19;
private final int DELETE_ID = 3;


@Autowired
IVehicleService vehicleService;

@Path("addVehicle")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Vehicle createVehicle() throws Exception {
vehicleService.addVehicle(vehicleNew);
return vehicleNew;
}
}


Thanks in advance

Answer

Check your configuration (either a class marked with @Confuguration or some xml file) for the @ComponenetScan annotation such as

@ComponentScan(basePackages = "com.testing")

or xml entry such as:

<context:component-scan base-package="com.testing" />

The component scan base package paths should be where your implementing classes should be located.

The exception you are getting tells you that the Spring container cannot find the implementing class.

SUGGESTION

Put your service and all implementing class in packages that are under com.testing base package

Then create a configuration class, something like this:

@Configuration
@ComponentScan(basePackages = "com.testing")    
public class MyConfiguration {

}

com.testing.services - put your VehicleService there

Comments