【问题标题】:could not autowire field spring-boot无法自动装配现场弹簧启动
【发布时间】:2014-08-30 00:24:18
【问题描述】:

需要一些帮助,我刚刚开始学习 Spring,似乎无法弄清楚我们错在哪里:

Application.java - 没有包

@Configuration
//@ComponentScan({"com.mapping","com.accesors","com.controllers"}) --Originaly tried this
@ComponentScan(basePackageClasses={UserDAO.class,Root.class,User.class})
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

User.java - 包 com.mapping

@Entity
public class User {

    @Id
    @GeneratedValue
    private Long id;
    private String userName;
    private String password;
    //accesors
}

UserDAO.java - 包 com.accesors

@Repository
public interface UserDAO extends JpaRepository<User, Long> {

}

Root.java - 包 com.controllers

@Controller
@RequestMapping("/*")
public class Root {

    @Autowired
    UserDAO userDAO;

    @RequestMapping("/")
    @ResponseBody
    public String rootAction(@RequestParam(defaultValue="foo") String name){
        List<User> lst = this.userDAO.findAll();
        for(User u : lst){
            System.out.println(u);
        }
        return "hello, " + name;
    }

}

当我运行项目时,我似乎得到了以下异常

堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'root': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.accesors.UserDAO com.controllers.Root.userDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.accesors.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at demo.Application.main(Application.java:14)

据我了解,这意味着 @ComponentScan 没有检测到包

【问题讨论】:

标签: java spring spring-mvc spring-boot spring-tool-suite


【解决方案1】:

如果您使用 springRunner测试您的 springboot 应用程序,请不要忘记包含 ComponentScan 注释。

@RunWith(SpringRunner.class)    
@SpringBootTest (classes = YourRestController.class )    
@ComponentScan({"your package goes here"})    
@AutoConfigureMockMvc    
@ActiveProfiles("development")    
public class Foo{
}

【讨论】:

    【解决方案2】:

    如果您的 Spring Data 存储库不在 @EnableAutoConfiguration 类的子包中,那么您需要添加 @EnableJpaRepositories(如果实体不在子包中,@EntityScan 也是如此)。

    【讨论】:

    • 这太棒了。我最初认为在 @componentScan 中包含这个包就可以了。但似乎您必须分别在@EnableJpaRepositories@EntityScan 中包含基本包之外的存储库和实体。谢谢。
    猜你喜欢
    • 2016-09-27
    • 1970-01-01
    • 2017-10-04
    • 2012-10-18
    • 2017-11-11
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多