Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Springbean在扩展的类中为null_Java_Spring - Fatal编程技术网

Java Springbean在扩展的类中为null

Java Springbean在扩展的类中为null,java,spring,Java,Spring,我已经用很多方法搜索过了,但是找不到任何与之相关的东西。 我遇到了一个问题,在另一个bean(扩展的)中没有Springbean被自动连接 我所拥有的是: interface A { } @Service class B implements A { @Autowired private RandomSpringBean randomSpringBean; } @Primary @Service class C extends B { } 现在发生的事情是,B中的

我已经用很多方法搜索过了,但是找不到任何与之相关的东西。 我遇到了一个问题,在另一个bean(扩展的)中没有Springbean被自动连接

我所拥有的是:

interface A {
}

@Service
class B implements A {
    @Autowired
    private RandomSpringBean randomSpringBean;
}

@Primary
@Service
class C extends B {
    
}
现在发生的事情是,B中的RandomSpringBean在被另一个自动连接B的类调用时并没有被自动连接,而只是被自动连接C的类调用,如下所示:

class D {
    @Autowired
    private B b;//beans inside B are null
}

class E {
    @Autowired
    private C c;//beans inside B (C subclass) are not null
}
D和E都在其他类中自动连接。 如果我注释掉/删除C,B将恢复正常工作

我希望这不会太令人困惑


我不是这样做的,是别人干的。我只是遇到了这个问题,想知道一种修复它的方法,也许不需要让C停止扩展B。

如果您没有使用
@Qualifier
/
@Primary
或类似的结构,这部分代码应该会给您带来问题:

class D {
     @Autowired
     private B b;
}
例外情况如下:

Field b in my.boring.package.beandemo.D required a single bean, but 2 were found:
下面是一个工作演示:

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class MyBeans {
    
    @Component
    public static class RandomBean {}
    
    public static interface A {}
    
    @Component("B")
    public static class B implements A {
        
        @Autowired
        RandomBean randomBean;
    }
    
    @Component
    public static class C extends B {}
    
    @Component
    public static class D {
        
        @Autowired
        @Qualifier("B")
        private B b;
        
        @PostConstruct
        public void checkRandomBean() {
            if (b.randomBean != null) {
                System.out.println("B has randomBean");
            }
        }
    }
    
    @Component
    public static class E {
        
        @Autowired
        private C c;
        
        @PostConstruct
        public void checkRandomBean() {
            if (c.randomBean != null) {
                System.out.println("C has randomBean");
            }
        }
    }
}
编辑:

请检查D中B的运行时类

因为C是用
@Primary
注释的,所以C的一个实例应该被注入到D中


另外,C将无法访问B的私有字段。

您的问题不是设置,您的问题很可能是如何创建D和E实例。您能提供包结构和bean配置类/xmls吗?我假设OP是说D和E是以相同的方式创建的。此外,如果它们都是自动连接的(无论是
b
还是
c
都是
null
),那么我看不出它们是如何被错误创建的。-但是拥有更多的信息当然不会有什么坏处。@Tom D和E实例都是在其他实例中自动连接的classes@CryptoFool您假设正确,它们都是自动连接的。正如我现在补充的,如果我删除C类,那么它工作正常。我现在更新了问题。我使用@Primary,但忘了在这里添加它。我在D中检查了B,它实际上是C的一个实例。我还将B的某些字段从private更改为protected,但它们仍然为null。