如何使自动装配注释起作用?

时间:2019-04-27 08:36:49

标签: java spring hibernate autowired

我正在尝试通过一些互联网课程来学习春季课程。我在@Autowired上遇到问题,但仍然出现错误:org.springframework.beans.factory.UnsatisfiedDependencyException

我发现了许多类似的问题,但是没有人适合我。

我的产品类别:

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

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

}

最后,我的属性文件:

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

2 个答案:

答案 0 :(得分:2)

您可能必须在SpringBootApplication类(或单独的配置类)中启用存储库:
https://www.concretepage.com/spring-boot/spring-boot-crudrepository-example

如果这不起作用,请确保SpringBootApplication类的包装比其他类的包装高,以便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

答案 1 :(得分:1)

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

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

谢谢您的帮助!