Cur*_*rge 7 spring javabeans mongodb
我在创建用于依赖注入的 bean 时遇到问题。这是场景。
我正在处理 MongoDB 存储库,我还创建了一个使用它的类。我正在尝试实例化两者的 bean 实例。
MongoDB 存储库:
@Repository
public interface ProductGlobalTrendRepository extends MongoRepository<ProductGlobalTrend,String>{
public ProductGlobalTrend findByPid(@Param("pId") String pId);
}
Run Code Online (Sandbox Code Playgroud)
使用它的类:
@Service
@Scope("singleton")
public class ProductTrendService {
@Autowired
@Qualifier("productGlobalTrendRepo")
ProductGlobalTrendRepository productGlobalTrendRepo;
public ProductTrendService() {
super();
}
public void setProductGlobalTrendRepo(
ProductGlobalTrendRepository productGlobalTrendRepo) {
this.productGlobalTrendRepo = productGlobalTrendRepo;
}
public ProductTrendService(ProductGlobalTrendRepository productGlobalTrendRepo) {
super();
this.productGlobalTrendRepo = productGlobalTrendRepo;
}
}
Run Code Online (Sandbox Code Playgroud)
spring 的 bean 配置 xml 具有以下条目:
<bean id="productTrendService" class="com.api.services.ProductTrendService"> </bean>
<bean id="productGlobalTrendRepo" class="com.mongodb.repository.ProductGlobalTrendRepository"> </bean>
Run Code Online (Sandbox Code Playgroud)
以下是我收到的错误:
19428 [localhost-startStop-1] 警告 org.springframework.web.context.support.AnnotationConfigWebApplicationContext - 上下文初始化期间遇到异常 - 取消刷新尝试 org.springframework.beans.factory.BeanCreationException:创建名称为“productTrendService”的 bean 时出错:注入自动装配依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.mongodb.repository.ProductGlobalTrendRepository com.api.services.ProductTrendService.productGlobalTrendRepo;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [com/vstore/conf/spring-security.xml] 中定义的名为“productGlobalTrendRepo”的 bean 时出错:bean 实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.mongodb.repository.ProductGlobalTrendRepository]:指定的类是一个接口
它抱怨存储库是一个接口类。
有人可以建议这个 bean 依赖注入的修复/解决方法吗?
问题出在上下文文件中的以下信息
<bean id="productGlobalTrendRepo"
class="com.mongodb.repository.ProductGlobalTrendRepository">
</bean>
Run Code Online (Sandbox Code Playgroud)
您应该创建一个类 com.mongodb.repository.ProductGlobalTrendRepositoryImpl ,它实现 com.mongodb.repository.ProductGlobalTrendRepository 并提供其方法的实现。
然后将你的bean声明信息更改为
<bean id="productGlobalTrendRepo"
class="com.mongodb.repository.ProductGlobalTrendRepositoryImpl">
</bean>
Run Code Online (Sandbox Code Playgroud)
在场景后面创建对象,这是界面不可能实现的。
| 归档时间: |
|
| 查看次数: |
28771 次 |
| 最近记录: |