Announcement Announcement Module
Collapse
No announcement yet.
Problem with Autowired: NoSuchBeanDefinitionException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Autowired: NoSuchBeanDefinitionException

    I got the following error when attempted to autowire a bean in the SimpleController class.

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleController': 
    Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire method: public void com.simple.web.SimpleController.setAccount(com.simple.busobj.Account); 
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No matching bean of type [com.simple.busobj.Account] found for dependency: 
    expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    Account.java
    Code:
    package com.simple.busobj;
    
    @Component
    public class Account
    {
    }

    SimpleController.java
    Code:
    package com.simple.web;
    
    @Controller
    @RequestMapping("/message")
    public class SimpleController {
    
    	private Account account;
    
    	@RequestMapping(method = RequestMethod.GET)
    	public String getMessage() {
    
    		return "simple";
    	}
    	
            @Autowired
    	public void setAccount(Account account)
    	{
    		this.account = account;
    	}
    }
    SimpleConfig.java
    Code:
    package com.simple.config;
    
    @Configuration
    @ImportResource({"classpath:simple-dao.xml", "classpath:simple-aspectj.xml"})
    public class SimpleConfig {
    	@Bean
    	public SimpleController simpleController() {
    
    		log.info("Getting the SimpleController");
    
    		return new SimpleController();
    	}
    }
    simple-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:component-scan base-package="com.simple"/>
    
    </beans>
    Last edited by zollen; Jan 26th, 2010, 08:49 PM.

  • #2
    I am using AnnotationConfigWebApplicationContext class.

    Comment


    • #3
      All beans must be declared either in spring config XML file or Configuration class (with @Bean), in order to be visible for @Autowired

      Comment

      Working...
      X