如何使自动装配注释工作?

How can I make Autowired adnotation working?

我正在尝试使用一些互联网课程来学习 spring。我在使用 @Autowired 时遇到问题,但仍然收到错误消息:org.springframework.beans.factory.UnsatisfiedDependencyException

我发现了很多类似的问题,但没有一个适合我的。

我的产品class:

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;
}
}

接口产品库:

import HIB_UD_01.product.entities.Product;
import org.springframework.data.repository.CrudRepository;


public interface ProductRepository extends CrudRepository<Product,    Integer> {
}

产品数据应用程序:

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);

}

}

我的测试class:

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);
}

}

最后,我的属性文件:

 spring.datasource.url=jdbc:mysql://localhost:3306/mydb
 spring.datasource.username=root
 spring.datasource.password=password

我应该将有关产品的数据放入数据库,但出现错误:

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 class(或在单独的配置 class 中)启用存储库:
https://www.concretepage.com/spring-boot/spring-boot-crudrepository-example

如果这不起作用,请确保您的 SpringBootApplication class 是一个高于其余 classes 的包,以便 SpringBoot 可以自动检测您的 bean。 (然后您可以尝试使用 @Repository 注释您的存储库,以确保 SpringBoot 自动检测您的存储库。)

另见:
https://dzone.com/articles/the-springbootapplication-annotation-example-in-ja
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html

好的,问题已解决。我将 @Repository 添加到 ProductRepository。我清除了存储库文件夹并下载了新的存储库。 然后我在连接 MySQL 时遇到错误。所以我编辑我的属性文件:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

感谢您的帮助!