gpt4 book ai didi

java - 如何使 Autowiring 注释工作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:01 26 4
gpt4 key购买 nike

我正在尝试使用一些互联网类(class)来学习 spring。我在使用 @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)产品库:

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

}

}

还有我的测试类:

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 类(或在单独的配置类中)启用存储库:
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

关于java - 如何使 Autowiring 注释工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55878737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com