私はSpring MVCで本当に新しいです。私はデモを書く(注釈を使う)が、うまくいきません。 Configure.javaで here is the construction of my demoSpring 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[] { "/" };
}
}
、私はこのように、 'ContackDAO' Beanを宣言します。MainControl.java .Iで
@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();
}
}
は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)}
私のデモで何が間違っていますか?手伝って頂けますか?
私はコード置けば見つける:RootConfigure.class
@Configuration
@ComponentScan(basePackages = { "base" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfigure {
@Bean
public ContactDAO getContactDAO() {
return new ContactDAOImpl();
}
}
に
@Bean
public ContactDAO getContactDAO() {
return new ContactDAOImpl();
}
を、successfully.I実行するデモはなぜ知らない...
いくつの設定クラスがありますか? –