我正在构建一个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
}
3条答案
按热度按时间3xiyfsfu1#
尝试按以下方式更改
Product
类:在
Product
类中,缺少@Entity
注解,productId
应为String
,如存储库中所指定。或者,如果要保留当前字段类型,请更改为
extends MongoRepository<Product, Integer>
。无论如何,它们应该匹配。
lmvvr0a82#
让您的Spring应用程序知道您的类所在的位置。使用
@ComponentScan(basePackages = "com.package")
。此外,如果你的产品类是你想要持久化的东西,那么用
@Entity
来标记它,这样Spring就知道了,并从中创建了一个Bean。用
@service
标记您的接口ProductService,以便Spring知道它。z9ju0rcb3#
不满足依赖异常:Repository.java这个页面就像下面的@respository注解类一样,返回类型为null和nearoptinal〈〉,使用default关键字这个方法对我有效。