Я действительно новый в Spring MVC. Я пишу демо (используйте аннотацию), но он не работает. here is the construction of my demoОшибка впрыскивания Spring Mvc (впрыскивание зависимых от автоподключения)
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', как это:
@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 .I нужно использовать 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();
}
}
, демонстрационная запустить successfully.I не знаю, почему ...
Сколько конфигурационных классов у вас есть? –