簡體   English   中英

如何使Autowired注釋工作?

[英]How can I make Autowired adnotation working?

我正在嘗試使用一些互聯網課程學習春天。 我有@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;
}
}

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

您可能必須在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

好的,問題已解決。 我將@Repository添加到ProductRepository。 我清除了存儲庫文件夾並下載了新的存儲庫。 然后我與MySQL的時區有錯誤。 所以我編輯我的專業文件:

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

謝謝您的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM