How can I make Autowired adnotation working?

dobrostaj :

I'm trying to learn spring using some internet courses. I have problem with @Autowired and I'm still getting Error: org.springframework.beans.factory.UnsatisfiedDependencyException

I have found many similar problems, but no one suits mine.

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

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

}

}

And my Test 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);
}

}

And last,my properites file:

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

I should put data about product into db, but I get an error:

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

You probably have to enable repositories on your SpringBootApplication class (or in a separate configuration class):
https://www.concretepage.com/spring-boot/spring-boot-crudrepository-example

If that doesn't work, make sure your SpringBootApplication class is a package higher then the rest of your classes, so SpringBoot can autodetect your beans. (And you can try to annotate your repository with @Repository then, to make sure SpringBoot autodetects your repository.)

Also see:
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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97720&siteId=1