Java 哪一个等同于在XML配置文件中自动连接带有@Autowire注释的接口?

Java 哪一个等同于在XML配置文件中自动连接带有@Autowire注释的接口?,java,xml,spring,spring-java-config,xml-configuration,Java,Xml,Spring,Spring Java Config,Xml Configuration,我看到用@Autowire注释注入ShopRepo是可行的,但当我尝试用xml时,有时可行,有时不行(intellij还说我不能使用抽象bean作为属性)。为什么它使用注释和xml配置并不总是有效(这是区别所在)? 我如何使它与xml配置一起工作 代码如下所示: public interface ShopRepo extends JpaRepository<Product, Long> { @Override Optional<Product> findB

我看到用@Autowire注释注入ShopRepo是可行的,但当我尝试用xml时,有时可行,有时不行(intellij还说我不能使用抽象bean作为属性)。为什么它使用注释和xml配置并不总是有效(这是区别所在)? 我如何使它与xml配置一起工作

代码如下所示:

public interface ShopRepo extends JpaRepository<Product, Long> {
    @Override
    Optional<Product> findById(Long aLong);
}

public class ShopController {

    //@Autowired
    private ShopRepo shopRepo;


    public void setShopRepo(ShopRepo shopRepo) {
        this.shopRepo = shopRepo;
    }

    public Product findProduct(Long id) {
        return shopRepo.findById(1l).orElse(new Product());
    }
}


    <jpa:repositories base-package="com.example.shop.repository"/>

<bean id="shopRepo" class="com.example.shop.repository.ShopRepo" abstract="true"/>

<bean id="shopController" class="com.example.shop.controller.ShopController">
    <property name="shopRepo" ref="shopRepo"/>
</bean>
公共界面ShopRepo扩展了JpaRepository{
@凌驾
可选findById(长沿);
}
公共类商店管理员{
//@自动连线
私人购物者购物者购物者购物者购物者购物者购物者;
公共证券回购(ShopRepo ShopRepo){
this.shopRepo=shopRepo;
}
公共产品findProduct(长id){
return shopRepo.findById(1l).orElse(新产品());
}
}

使用@Autowire时,实际上是在按类型执行Autowire@Autowire只是注入ShopRepoBean的实现。shopRepo的实现通常在spring容器启动期间由jpa存储库动态实例化

您的xml配置没有按类型进行任何自动连接,而是试图将id为“shopRepo”的bean注入shopcontroller bean。xml中的shopRepo定义只是一个定义,而不是jpa存储库创建的实际实现的名称

您可以在xml文件中遵循这一点。希望这有帮助

<bean id="shopRepo" class="com.example.shop.repository.ShopRepo" abstract="true"/>
<bean id="shopController" class="com.example.shop.controller.ShopController" autowire="byType">   
</bean>

考虑将xml定义从java代码中分离出来,在两者之间写一些东西,例如“xml中的JPA映射”。