简体   繁体   中英

No matching bean of type found for dependency: Spring MVC

I am getting this error when trying to use @autowire, @configuration, @bean, @Repository in my Spring MVC project

Could not autowire field: private com.sachin.dao.StockDaoImpl com.sachin.myapp.HomeController.stockDao;

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.sachin.dao.StockDaoImpl] found for dependency:

Please let me know what mistake I am making. I am new to Spring MVC and dependency injection.

Here is my controller code. I am trying to inject StockDaoImpl in the controller.

@Controller
public class HomeController {

     @Autowired
    private StockDaoImpl stockDao;

    @RequestMapping(value = "/stockgoogle/", method = RequestMethod.GET)
    public @ResponseBody Stock stockGoogle(Locale locale, Model model) {    
        //StockDaoImpl stockDao = new StockDaoImpl();
        Stock s=stockDao.listGoogle();
        model.addAttribute("s", s );        
        return s;
    }

}

My Service Implementation is below. I have used @Repository annotation here with "stockDao" which is my variable name in controller that I want to inject

@Repository("stockDao")
public class StockDaoImpl implements StockDao { 
    @Override
    public Stock listGoogle() {
        Stock s = null;
        try {
            ... //some code
            String  name = rs.getString("Name");
            s = new Stock(name);
            ...
         } catch (Exception e) {
         }
         return s;
    }
}

Also I have created a configuration class separately. I am using this to define my bean. I am only using this to specify bean and have not imported it anywhere in the code.

@Configuration
public class BeanConfiguration {
    @Bean
    public StockDaoImpl stockDao(){
      return new StockDaoImpl();
    }
}

Am I missing something here. From looking at the error it looks like the @Bean annotation is not visible to the factory. Do I have to do anything else other than annotating the @configuration class.

I might also be using the annotations in a wrong way. I could be making a mistake in how I am using @Autowired or @Repository.

Can you please help.

I think this might be your issue:

"Also I have created a configuration class separately. I am using this to define my bean. I am only using this to specify bean and have not imported it anywhere in the code."

Somewhere you need to tell Spring to look for BeanConfiguration . You can do this in your applicationContext.xml file (assuming you have one) as follows:

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

This assumes BeanConfiguration is in the com.sachin.config package.

If you can't find where to put this it may be helpful to share your web.xml file.

I am having the described behaviour in a test class.

I work in IntelliJ 2020.1, with an old project build on:

Java 1.6
Spring 3.0.5.RELEASE
Maven POM 4.0.0
Maven 3.2.5

The test class starts as:

@Test
@ContextConfiguration(locations={ "classpath*:beans_sets/UhradyForIns.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=false)
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class OdeslatJakobyAutomatickyUhraduTest extends TestBaseWithProperties {
    private static final Logger log = LoggerFactory.getLogger(OdeslatJakobyAutomatickyUhraduTest.class);
    @Autowired
    U01UhradaBaseJpaDaoForTesting dao;

Sometimes suddenly, after some changes in POMs, Spring cannot autowire the dao giving exactly your messages. The dao is well described by its class and by XML context description but is suddenly invisible for Spring. I have tried many ways described on SO, adding annotations, changing configuration, but I have found, that it is all excessive. All I need is only to refresh the maven repositories 4 times by 4 different orders subsequently:

1. mvn -e -U clean install         //In cmd line
2. Ctrl+Shift+A Reimport maven     //in IntelliJ
3. double round arrows for refresh //in the maven window of IntelliJ
4. AGAIN REPEAT THE FIRST REFRESH. 

After that, it is all OK and works fine till the next greater change in POMs.

Or... Till due to some inner reasons, Maven or IntelliJ damages the local jar repository without your participation. At least, I had a case, when I haven't touched the repository, but again that Could not autowire... message appeared. But this time only one

mvn -e -U clean install 

was enough.

Obviously, there is/are some error(s) in the maven and/or maven plugin in IntelliJ. That problem is shown by Could not autowire... message. The repair is possible by a simple maven repository refresh or, in worse cases, demand a sequence of several different repository refreshes.

I understand that it has no obvious logic in that solution, and our developer's work resembles magic more and more. And this time, definitely, it is not your or my vice. Simply we have to adapt to existing errors. At least we know how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM