【问题标题】:Spring Mvc Injection Error(Injection of autowired dependencies failed)Spring Mvc 注入错误(自动装配依赖项的注入失败)
【发布时间】:2016-05-06 05:13:05
【问题描述】:

我是 Spring MVC 的新手。我写了一个演示(使用注释),但它不起作用。 here is the construction of my demo

public class WebInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfigure.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { Configure.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

在 Configure.java 中,我声明了 'ContackDAO' bean,如下所示:

@Configuration
@ComponentScan(basePackages = "base")
@EnableWebMvc
public class Configure extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver getViewResolver() {
    InternalResourceViewResolver resolver = new   InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations(
            "/resources/");
}

@Bean
public ContactDAO getContactDAO() {
    return new ContactDAOImpl();
}
} 

在 MainControl.java 中。我需要使用 ContackDAO,所以我写了这个:

@Controller
public class MainControl {
@Autowired
private ContactDAO contactDAO;

@RequestMapping(value="/")
public String listContact() throws IOException{
    System.out.println(contactDAO.getBean());// getBean() returns "hello"
    return "hello";
}
}

当我运行这个演示时,我得到了错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainControl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private base.dao.ContactDAO base.contorl.MainControl.contactDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [base.dao.ContactDAO] 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)}

我的演示有什么问题?你能帮帮我吗?

如果我把代码我发现:

@Bean
public ContactDAO getContactDAO() {
    return new ContactDAOImpl();
}

进入 RootConfigure.class

@Configuration
@ComponentScan(basePackages = { "base" }, excludeFilters = { @Filter(type =     FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfigure {
@Bean
public ContactDAO getContactDAO() {
    return new ContactDAOImpl();
}
}

,demo运行成功,不知道为什么...

【问题讨论】:

  • 你有多少个配置类?

标签: java spring spring-mvc


【解决方案1】:

我希望你没有错过 contactDAO 的二传手。

【讨论】:

  • @Autowire 需要它才能工作。更多info
  • 他没有使用 setter 自动装配,他使用 @Bean 注释定义 bean。这种类型的自动装配很好。
  • 当您在它自己的元素上提及@Autowire 时,您不需要设置器。
【解决方案2】:

在回答您的问题之前,一个好的做法是遵循正确的包结构。

i.e. in Your case you can put the configuration like this:

        base.dao.config 
        base.dao.impl

        base.mvc.config
        base.mvc.impl

然后将所有这些都包含在最顶层的配置中。这样,对于每种类型的 bean 的配置,这将是一个清晰的分离。 再次为@Repository@Service@Controller

回答你关于为什么的问题。因为您正在使用 excludeFilters 在 RootConfig.java 中排除您的 mvc 配置类。

检查 RootConfig.java 中excludeFilters 的链接

您需要首先了解 @EnableWebMvc 的作用以及 excludeFilters 的作用,这些在下面的链接中进行了说明

ComponentScan

与类似事物相关的一些 SO 问题:

excludeFilters not working

还有一个: Filter Specific Packages in Component Scan

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2012-07-03
    相关资源
    最近更新 更多