Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type - Spring boot

unsatisfied dependency expressed through field
unsatisfied dependency expressed through field 'sessionfactory
required a bean of type that could not be found
unsatisfied dependency expressed through method 'setcontentnegotationstrategy' parameter 0
error creating bean with name spring boot
org.springframework.beans.factory.unsatisfieddependencyexception error creating bean with name junit
could not autowire no beans of type found
org springframework beans factory annotation autowired(required=true)

I'm using Spring-boot to implement a Java application. One of the controllers is callled HealthCheckerController, defined in the application package:

HealthCheckerController.java:

package application;

import healthchecker.Instance;
import healthchecker.InstanceRepository;

@RestController
public class HealthCheckerController {

    @Autowired
    private TopRepository topRepository;

    @Autowired
    private InstanceRepository instanceRepository;

    @SuppressWarnings("unused")
    private final static Logger logger = Logger.getLogger(HealthCheckerController.class);

    public HealthCheckerController(InstanceRepository instanceRepository) {
        this.instanceRepository = instanceRepository;
    }
   // other things not important here.
}

In the healthchecker package, I have Instance and InstanceRepository defined:

package healthchecker;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "instances", path = "instances")
public interface InstanceRepository extends MongoRepository<Instance, String> {

    Instance findByIpaddress(@Param("ipaddress") String ipaddress);

    List<Instance> findByAmi(String ami);
}

And the Instance model:

package healthchecker;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

@Document(collection = "instances")
public class Instance {

    @Id
    @JsonSerialize(using = ToStringSerializer.class)
    private String id;

    private String ipaddress;
    private String ami;
    // .. 
}

Now, to test the controller, I have the following Unit Test:

package application;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.http.MockHttpOutputMessage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.google.gson.Gson;

/* Import local libraries */
import healthchecker.Instance;
import healthchecker.InstanceRepository;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class HealthCheckerControllerTest {

    private MockMvc mvc;

    private final static Logger logger = Logger.getLogger(HealthCheckerControllerTest.class);

    @Autowired
    private InstanceRepository instanceRepository;

    private HttpMessageConverter<Object> mappingJackson2HttpMessageConverter;

    private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
            MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));

    @Autowired
    void setConverters(HttpMessageConverter<?>[] converters) {

        @SuppressWarnings("unchecked")
        HttpMessageConverter<Object> httpMessageConverter = (HttpMessageConverter<Object>) Arrays.asList(converters)
                .stream().filter(hmc -> hmc instanceof MappingJackson2HttpMessageConverter).findAny().get();
        this.mappingJackson2HttpMessageConverter = httpMessageConverter;

        Assert.assertNotNull("the JSON message converter must not be null", this.mappingJackson2HttpMessageConverter);
    }

    @Before
    public void setUp() throws Exception {
        this.mvc = MockMvcBuilders.standaloneSetup(new HealthCheckerController(this.instanceRepository)).build();

        this.instanceRepository.deleteAll();
        Instance first = new Instance("10.20.30.40", "ami-xxxxxxxx");
        Instance second = new Instance("10.20.30.41", "ami-xxxxxxxx");
        this.instanceRepository.save(first);
        this.instanceRepository.save(second);

        for (Instance instance : this.instanceRepository.findAll()) {
            logger.info(instance);
        }
    }
   @Test
    public void testHealthChecker() throws Exception {
        this.mvc.perform(get("/healthchecker")).andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$", hasSize(2)))
                .andExpect(jsonPath("$[0].ipaddress", is("10.20.30.40")))
                .andExpect(jsonPath("$[0].ami", is("ami-xxxxxxxx")))
                .andExpect(jsonPath("$[1].ipaddress", is("10.20.30.41")))
                .andExpect(jsonPath("$[1].ami", is("ami-xxxxxxxx")));
    }
   // other things here
  }

And I'm getting the following exception:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthCheckerController' defined in file [/Users/philippesouzamoraesribeiro/Documents/workspace/cloudos/target/classes/cloudos/HealthCheckerController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [healthchecker.InstanceRepository] found for dependency [healthchecker.InstanceRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [healthchecker.InstanceRepository] found for dependency [healthchecker.InstanceRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthCheckerController' defined in file [/Users/philippesouzamoraesribeiro/Documents/workspace/cloudos/target/classes/cloudos/HealthCheckerController.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [healthchecker.InstanceRepository] found for dependency [healthchecker.InstanceRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [healthchecker.InstanceRepository] found for dependency [healthchecker.InstanceRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111) [spring-boot-test-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115) [junit-4.12.jar:4.12]
    at org.testng.junit.JUnit4TestRunner.start(JUnit4TestRunner.java:81) [testng-6.9.8.jar:?]
    at org.testng.junit.JUnit4TestRunner.run(JUnit4TestRunner.java:69) [testng-6.9.8.jar:?]
    at org.testng.TestRunner$1.run(TestRunner.java:689) [testng-6.9.8.jar:?]
    at org.testng.TestRunner.runWorkers(TestRunner.java:1014) [testng-6.9.8.jar:?]
    at org.testng.TestRunner.privateRunJUnit(TestRunner.java:720) [testng-6.9.8.jar:?]
    at org.testng.TestRunner.run(TestRunner.java:621) [testng-6.9.8.jar:?]
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) [testng-6.9.8.jar:?]
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) [testng-6.9.8.jar:?]
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) [testng-6.9.8.jar:?]
    at org.testng.SuiteRunner.run(SuiteRunner.java:261) [testng-6.9.8.jar:?]
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) [testng-6.9.8.jar:?]
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) [testng-6.9.8.jar:?]
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191) [testng-6.9.8.jar:?]
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1116) [testng-6.9.8.jar:?]
    at org.testng.TestNG.run(TestNG.java:1024) [testng-6.9.8.jar:?]
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:115) [surefire-testng-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:212) [surefire-testng-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:108) [surefire-testng-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111) [surefire-testng-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) [surefire-booter-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) [surefire-booter-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [surefire-booter-2.18.1.jar:2.18.1]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [healthchecker.InstanceRepository] found for dependency [healthchecker.InstanceRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    ... 61 more

Everything was working before, the change I made was to move Instance and InstanceRepository from application to healthchecker, because I wanted to leave only controllers / website related utilities in application. Since I'm using Spring-Boot 1.4.0 and Java 1.8, I haven't done any beans configuration. How can I fix this problem?


Try adding @ComponentScan("healthchecker") so Spring knows where the other beans are located.

Unsatisfied Dependency in Spring, UnsatisfiedDependencyException gets thrown when, as the name This may happen when Spring application tries to wire a bean and cannot When we run this Spring Boot application, everything works fine. Unsatisfied dependency expressed through constructor parameter 0 Package Not Scanned. UnsatisfiedDependencyException gets thrown when, as the name suggests, some bean or property dependency isn't satisfied. This may happen when Spring application tries to wire a bean and cannot resolve one of the mandatory dependencies.


Try this in the place where you have your configuration file/@SpringBootApplication:

@ComponentScan("application")
@EnableJpaRepositories("healthchecker")
@EntityScan("healthchecker")

This worked for me

Spring NoSuchBeanDefinitionException, No qualifying bean of type [com.baeldung. by Spring: “expected at least 1 bean which qualifies as autowire candidate for this dependency”. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.sci.pascal.core.services.AccountService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.


Parameter 0 of constructor in org.springframework.data.jpa , After migrating to SpringBoot 1.4.1, Spring Data Hopper-SR3 has updated and all projects Unsatisfied dependency expressed through constructor parameter 0; nested NoSuchBeanDefinitionException: No qualifying bean of type 'java.util. Why GitHub? Features →. Code review; Project management; Integrations; Actions; Packages; Security


Autowired repository Bean creation fails: Unsatisfied dependency , GitHub is home to over 50 million developers working together to host and Autowired repository Bean creation fails: Unsatisfied dependency expressed through constructor parameter 1: Ambiguous argument values for parameter of type RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.0​.0. Hi guys, me too this bug after move my project from 1.4.0 spring boot data to 1.4.1, details: 1. 2016-09-29 19:40:57,035 ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter : ***** APPLICATION FAILED TO START ***** Description: Parameter 0 of constructor in org.springframework.data.jpa.repository.support.DefaultJpaContext required a bean of type 'java.util.Set' that could


Error creating bean with name 'xxxx': Unsatisfied dependency , 'xxxx': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory. NoSuchBeanDefinitionException: No qualifying bean of type 'com.atlassian.event​.api. In this case, there is no bean definition for “someBeanName” – leading to the following exception: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'someBeanName' is defined. Again, Spring clearly and concisely indicates the reason for the failure: “No bean named X is defined“. 6. Cause


UnsatisfiedDependencyException, Other useful introductions include OSGi, Spring and the Plugin Framework and MyPlugin': Unsatisfied dependency expressed through constructor argument with UserManager]: : No unique bean of type [com.atlassian.sal.api.user. Note that the first argument to the constructor has index 0, not index 1. I am trying to do a basic CRUD API using Spring Boot, Hibernate, and MySQL. Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean