2017-12-17 8 views
0

私は単純なスプリングブートアプリケーションを使用しており、Hibernateを使用してデータベースに接続したいとします。 RepositoryクラスをControllerクラスに挿入することができません。助けてください。しかし、コードの原因となる問題を取り除くと、アプリケーションがすべてのコンポーネントをスキャンして正常に動作するようになります。 Eclipseでコードを作成しました。依存関係のあるタイプの修飾Beanがありません:Spring Boot

マイスタータークラス

package spring.starter; 
@SpringBootApplication 
@ComponentScan(basePackages = "spring") 
public class Starter { 

public static void main(String args[]) { 

    ApplicationContext ctx = SpringApplication.run(Starter.class, args); 

    } 

} 

私のコントローラクラス

package spring.controller; 
@RestController 
@RequestMapping("/app") 
public class AdminController { 

/*@Autowired 
private AdminService service;*/ 

@Autowired 
@Qualifier("adminRepository") 
private AdminRepository adminRepository; 

@GetMapping("/admin") 
public List<Admin> getAllAdmin() { 
    return adminRepository.findAll(); 
} 
} 

マイリポジトリクラス:このクラスのために私は、注釈が、それもくぼみ仕事の両方を削除しようとしています。

package spring.repository; 
@Repository 
@Qualifier("adminRepository") 
    public interface AdminRepository extends JpaRepository<Admin, Integer>{ 

} 

例外トレース

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.repository.AdminRepository] found for dependency [spring.repository.AdminRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=adminRepository)} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 
... 19 common frames omitted 

答えて

0

春データJPAリポジトリはComponentScan注釈によって検出されていません。彼らにとって、あなたは注釈

@EnableJpaRepositories(basePackages = "spring") 

を提供するために、どちらか持っているか、次のようSpringBootApplication注釈を変更します。

@SpringBootAnnotation(scanBasePackages = "spring") 

これは、その後、同様EnableJpaRepositoriesとしてComponentScanためのカスタムパッケージ構成をカバーします。 これらの変更のいずれかを行った後、@Repository をリポジトリから削除することもできます。そのアノテーションはSpring Data JPAリポジトリインタフェース用ではなく、影響を受けません。

+0

Annotationが記述されているため、SpringBootApplication(scanBasePackages = "spring")に変更することを意味しましたか?私はあなたの両方の方法を試みたが、それでも私は同じ例外を与えた。 –

0

私は、問題は、私のスタータークラスは私がしなければならなかったすべては、例えば、パッケージ構造の1つの上のレベルアップにこのクラスを移動することですパッケージspring.starterにあったが見つかりました:

package spring; 

@SpringBootApplication 
@ComponentScan(basePackages = "spring") 
public class Starter { 

    public static void main(String args[]) { 

     ApplicationContext ctx = SpringApplication.run(Starter.class, args); 

    } 

} 

し、それ働いた。しかし、私はここで疑いがあります。 @ComponentScanのメリットは何ですか?

+0

SpringBootApplicationアノテーションは、Configuration、EnableAutoConfiguration、およびComponentScanをデフォルト属性とともに使用することと同等です。 この起動アプリケーションクラスに含まれるすべてのBeanは、コンポーネントスキャンの対象となります。コンポーネントスキャンは省略できます。詳細はこちら[https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html]を参照してください。 – abhi3232

関連する問題