Spring MVC REST API:org.springframework. bean.工厂,不满足依赖项异常:

9wbgstp7  于 2023-03-08  发布在  Spring
关注(0)|答案(3)|浏览(88)

我正在构建一个API,似乎无法摆脱org.springframework.beans.factory.UnsatisfiedDependencyException,我无法发现其中的问题。我希望对此有新的见解。
p.s:使用mongo DB进行此操作
先排除全部误差

2020-05-04 01:16:31.468  WARN 14412 
--- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - 
cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'productController': 
Unsatisfied dependency expressed through field 'productService'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name 'productServiceImpl':
 Unsatisfied dependency expressed through field 'productRepo'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'productRepo': 
Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException:
 No property id found for type Product!
public class Product {

    @Id
    private int ProductId;

// and other variables plus setters and getters
@Repository
public interface ProductRepo extends MongoRepository<Product, String> {

   // methods
}
public interface ProductService {
 // methods
}
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepo productRepo;

// implementations
}
3xiyfsfu

3xiyfsfu1#

尝试按以下方式更改Product类:

@Entity
public class Product {

    @Id
    private String productId; // MongoRepository<Product, String>

    // methods
}

Product类中,缺少@Entity注解,productId应为String,如存储库中所指定。
或者,如果要保留当前字段类型,请更改为extends MongoRepository<Product, Integer>
无论如何,它们应该匹配。

lmvvr0a8

lmvvr0a82#

让您的Spring应用程序知道您的类所在的位置。使用@ComponentScan(basePackages = "com.package")
此外,如果你的产品类是你想要持久化的东西,那么用@Entity来标记它,这样Spring就知道了,并从中创建了一个Bean。
@service标记您的接口ProductService,以便Spring知道它。

z9ju0rcb

z9ju0rcb3#

不满足依赖异常:Repository.java这个页面就像下面的@respository注解类一样,返回类型为null和nearoptinal〈〉,使用default关键字这个方法对我有效。

相关问题