私はいくつかのインターネットのコースを使用して春を習得しようとしています。私は@Autowiredに問題があると私はまだエラーを取得しています:org.springframework.beans.factory.UnsatisfiedDependencyExceptionを
私は多くの同様の問題を発見したが、誰のスーツは私はありません。
私のProductクラス:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table
public class Product {
@Id
private int id;
private String name;
@Column(name = "description")
private String desc;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
インタフェースProductRepository:
import HIB_UD_01.product.entities.Product;
import org.springframework.data.repository.CrudRepository;
public interface ProductRepository extends CrudRepository<Product, Integer> {
}
ProductdataApplication:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductdataApplication {
public static void main(String[] args) {
SpringApplication.run(ProductdataApplication.class, args);
}
}
そして、私のテストクラス:
import HIB_UD_01.product.entities.Product;
import HIB_UD_01.product.repos.ProductRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductdataApplicationTests {
@Autowired
ProductRepository repository;
@Test
public void contextLoads() {
}
@Test
public void testCreate() {
Product product = new Product();
product.setId(1);
product.setName("Iphone");
product.setDesc("Awesome");
product.setPrice(1000d);
repository.save(product);
}
}
そして最後に、私のproperitesファイル:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
私は、DBへの製品に関するデータを置く必要がありますが、私はエラーを取得します:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'HIB_UD_01.product.ProductdataApplicationTests': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'HIB_UD_01.product.repos.ProductRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
あなたはおそらくあなたのSpringBootApplicationクラス上のリポジトリを有効にする必要があります(または別の構成クラスで):
https://www.concretepage.com/spring-boot/spring-boot-crudrepository-example
それがない作業を行う場合は、SpringBootはあなたの豆を自動検出できるように、SpringBootApplicationクラスは、高い、あなたのクラスの残りのパッケージであることを確認してください。(そして、あなたSpringBootがあなたのリポジトリを自動検出を確認するために、その後、@Repositoryでリポジトリに注釈を付けることを試みることができます。)
参照してください:
https://dzone.com/articles/the-springbootapplication-annotation-example-in-ja
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-構造化-あなたの-code.html