コード ```@Autowiredを使うことが出来ません。 すべてのクラスの同じpackageに入れて@Autowiredを使用しても下記のエラー文が出力されてプログラムが実行できません。 @ComponentScanを使用しても変わりません。 エラーが起こっている部分のプログラム文は下記の文です。 ```java @Service @Transactional public class CustomerService { @Autowired CustomerRepository customerRepository; public List<Customer> findAll(){ System.out.println("サービスfindAll()"); return customerRepository.findAllOrderByName(); } }
2017-07-24 11:29:21.356 WARN 5956 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerService': Unsatisfied dependency expressed through field 'customerRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'soturon.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2017-07-24 11:29:21.358 INFO 5956 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2017-07-24 11:29:21.382 INFO 5956 --- [ main] utoConfigurationReportLoggingInitializer : Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2017-07-24 11:29:21.457 ERROR 5956 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field customerRepository in soturon.CustomerService required a bean of type 'soturon.CustomerRepository' that could not be found. Action: Consider defining a bean of type 'soturon.CustomerRepository' in your configuration.
申し訳ありません。以下CustomerRepositoryクラスになります。
java
1package soturon; 2 3import java.util.List; 4 5import org.springframework.data.jpa.repository.JpaRepository; 6import org.springframework.data.jpa.repository.Query; 7 8public interface CustomerRepository extends JpaRepository<Customer, Integer> { 9 10 @Query("SELECT * FROM customer") 11 List<Customer> findAllOrderByName(); 12 13 14 15}
以下、動作確認で作ったソースです。
java
1@Controller 2public class MemoController { 3 4 @Autowired 5 CustomerService cumser; 6 7 @RequestMapping("/") 8 public String memo(){ 9 cumser.aaa(); 10 return "MemoView"; 11 } 12 13 14@Service 15@Transactional 16public class CustomerService{ 17 //ためし 18 public void aaa(){ 19 20 System.out.println("できたー"); 21 } 22}```
回答1件
あなたの回答
tips
プレビュー