2016-05-06 5 views
0

私は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実行するデモはなぜ知らない...

+0

いくつの設定クラスがありますか? –

答えて

0

I contactDAOのセッターが不足していないことを願っています。

+0

@Autowireが動作するために必要です。 [info](http://www.tutorialspring.com/spring/spring_autowired_annotation.htm) –

+0

彼はセッターのオートワイヤリングを使用していないので、彼は '@ Bean'アノテーションを使ってBeanを定義しています。このタイプのオートワイヤリングは問題ありません。 – rapasoft

+0

要素の上に@Autowireを記述しているときは、セッターは必要ありません。 –

0

質問に答える前に、適切なパッケージ構造に従うことをお勧めします。

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

     base.dao.config 
     base.dao.impl 

     base.mvc.config 
     base.mvc.impl 

これらのすべてを一番上の設定に含めます。この方法は、それは理由について、あなたの質問に答えるために@Repository@Service@Controller

のために再び ある豆の種類ごとに設定についての明確な分離になります。 excludeFiltersを使用してRootConfig.java内のmvc構成クラスを除外しているためです。

チェック

は、あなたが最初の@EnableWebMvcが何をするかを理解する必要がありRootConfig.javaでexcludeFiltersのリンクと何excludeFiltersは

ComponentScan

いくつかのSOの質問のリンクの下で説明されていないました類似物に関する:

excludeFilters not working

もう1つ: Filter Specific Packages in Component Scan

+0

あなたの答えをありがとう! –

関連する問題