Java – How do I make Autowiring annotations work?

How do I make Autowiring annotations work?… here is a solution to the problem.

How do I make Autowiring annotations work?

I’m trying to use some internet classes to learn spring. I’m having trouble with @Autowired and I still get the error: org.springframework.beans.factory.UnsatisfiedDependencyException

I found a lot of similar issues, but none of them worked for me.

My Product Category:

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 Product Library:

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

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

Product data applications:

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

}

Finally, my properties file:

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

I

should put data about the product into the database, 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)}

Solution

You might have to enable the repository:
in the 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 one package higher than the others so that SpringBoot can automatically detect your bean. (Then you can try annotating your repository with @Repository to ensure that SpringBoot automatically detects your repository.) )

See also:
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

Related Problems and Solutions