2017-07-09 9 views
0

コンポーネントスキャンと@componentにもパッケージ名を入れましたが、まだエラーが発生しています。コードは以下の通りです。Springでの自動ワイヤリングにエラーが発生しました

ここSpringConfigファイルされる: -

@Configuration 
@ComponentScan(basePackages={"com.cagataygurturk.example.lambda","com.cagataygurturk.example.services"}) 
public class SpringConfig { 

} 

ここでは、サービスクラスである: - ここに

@Component 
public class Service { 

    /** 
    * Autowiring another Spring Bean 
    */ 
    @Autowired 
    AnotherService anotherService; 

    public String getText(String text) { 
     //return anotherService.getText(text); 
     return "hello"; 
    } 
} 

は、サービス・クラスでautowiredさAnotherServiceクラスである: -

@Component 
public class AnotherService { 
    @Autowired 
    IFileStoreService file; 
    public String getText(String text) { 
     String t; 
     t=(String)text; 
     if(t.equals("get")) 
     { 
      file.get("1"); 
      return "You are in Get Controller and database is not connected\n"; 
     } 
     else 
     if(t=="post") 
     { 
      return "You are in post Controller and databse is not connecte\n"; 
     } 
     else 
     if(t=="delete") 
     { 
      return "You are int delete Controller and mongo database in not connected\n"; 
     } 
     else 
     { 
      return "hii\n"+text+"hey"; 
     } 
    } 
} 

AnotherServiceクラスでautowiredされているIFileStoreServiceクラスは次のとおりです。

ここで
public interface IFileStoreService { 
Resource get(String id) throws StorageException, StorageFileNotFoundException; 
} 

IFileStoreImplクラスである: -

@Component 
public class FileStoreServiceImpl implements IFileStoreService { 
    private static final Logger logger = LoggerFactory.getLogger(FileStoreServiceImpl.class); 

    @Autowired 
    private IFileStoreDAO fileStoreDAO; 

    @Autowired 
    private StorageProperties storageProperties; 


    @Override 
    public Resource get(String id) throws StorageException, StorageFileNotFoundException { 
     Resource resource = null; 
     File file = null; 
     try { 
      FileDetails fileDetails = fileStoreDAO.get(id); 
      if(fileDetails != null) { 
       String tempDir = storageProperties.getLocation(); 
       file = new File(tempDir + File.separator + fileDetails.getName()); 
       file.mkdirs(); 
       if(file.exists()) { 
        // p1: delete any file if existent in the directory; 
        file.delete(); 
       } 
       file.createNewFile(); 
       FileCopyUtils.copy(fileDetails.getFileBytes(), file); 
       resource = new UrlResource(Paths.get(tempDir).resolve(fileDetails.getName()).toUri()); 
      } else { 
       throw new StorageFileNotFoundException("No document found with id: " + id); 
      } 
     } catch (Exception e) { 
      logger.error(e.getMessage(), e); 
      if (e instanceof StorageFileNotFoundException) { 
       throw (StorageFileNotFoundException) e; 
      } else { 
       throw new StorageException("", e); 
      } 
     } 
     return resource; 
    } 
} 

そして最後MainHandler機能: -

@SuppressWarnings("unused") 
public class MainHandler 
     extends AbstractHandler<SpringConfig> 
     implements RequestHandler<Map<String,Object>, String> { 

    static final Logger log = Logger.getLogger(MainHandler.class); 
    @Override 
    public String handleRequest(Map<String, Object> input, Context context)throws RuntimeException { 
     // TODO Auto-generated method stub 
     Service businessService = getApplicationContext().getBean(Service.class); 
     return businessService.getText("hii"); 

    } 
} 

とエラーは次のとおりです。 - 例外スタックトレースとして

{"errorMessage":"Error creating bean with name 'service': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cagataygurturk.example.services.AnotherService com.cagataygurturk.example.services.Service.anotherService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cagataygurturk.example.services.IFileStoreService com.cagataygurturk.example.services.AnotherService.file; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileStoreServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.cagataygurturk.example.services.IFileStoreDAO com.cagataygurturk.example.services.FileStoreServiceImpl.fileStoreDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.cagataygurturk.example.services.IFileStoreDAO] 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)}","errorType":"org.springframework.beans.factory.BeanCreationException"} 
+0

とのインタフェース(IFileStoreService)を注釈付けてみてください 'を代わりに実装のComponent' @ – maydawn

+0

完了、それでも同じエラーが – rockz

+0

オーケーIが来ていますお詫び申し上げます、私はエラーを誤解しました。私は問題の根本が、私が考える最後の失敗依存性注入だと思う。エラーの最後の部分には、 'タイプ[com.cagataygurturk.example.services.IFileStoreDAO]の修飾Beanはありません。 ここでは投稿していないIFileStoreDAOクラスがコンポーネントスキャンの下にあり、正しく注釈付けされていますか? – maydawn

答えて

1

提案:

0123クラス IFileStoreDAOは(私はこれで100%わからないんだけど、別の例外を取得する可能性があります)の実装を持っている、または @Component注釈の欠落、またはによってコンポーネントとしてのSpringによってスキャンされていないいないことを意味

No qualifying bean of type [com.cagataygurturk.example.services.IFileStoreDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

@ComponentScan(basePackages={"com.cagataygurturk.example.lambda","com.cagataygurturk.example.services"})で宣言されていないパッケージに含まれています。

春ブートコンポーネントスキャンの詳細については、この回答を参照してください。https://stackoverflow.com/a/33619632/5229041

関連する問題