java - spring - 组件在一个类中 Autowiring ,但不在另一个类中 Autowiring

标签 java spring autowired

我遇到了一个奇怪的问题,其中注入(inject)了 @AutowireComponent 在一个类中可用,但在另一个类中不可用。

我在AccountAgreement类的属性network中使用@Autowired,但它是 Autowiring 的仅在Agreement类中,但不在Account类中。 @ComponentScan 运行 3 个所需的包。

这是开始类(class):

package com.ser.pm.tests;

@SpringBootApplication
@ComponentScan("com.ser.pm.agreement, com.ser.pm.network, com.ser.pm.address")
public class zt_Agreement {

    public static void main(String[] args) throws SignatureException, InterruptedException {

            ApplicationContext ctx = SpringApplication.run(zt_Agreement.class, args);

            Agreement oagreement = ctx.getBean(Agreement.class);

            oagreement.SubmitAgreement();

    }
}

这是组件 Network,它也必须注入(inject)到Account->network

package com.ser.pm.network;

@Component
public class Network implements INetwork {

    public X2network xsnetwork;

    public Network() { xsnetwork = networkFactory.createnetwork(); }

    public boolean isBound() { return (xsnetwork != null); }

    public BigInteger getNumber(byte[] addr) { return xsnetwork.getNumber(addr); }

}

在此类中,字段network 未 Autowiring :

package com.ser.pm.address;

@Component
public class Account implements IFSAccount {

    @Autowired
    Network network;

    public Account(String ir_Address, String ir_Name) {}

    public Account() {}


    public BigInteger getNumber() {
        return network.getNumber(Hex.decode(this.getAddress()));
    }

}

在此类中,字段network已正确自动连接

package com.ser.pm.agreement;

@Component
public class Agreement {

    protected Account oFSEthAddress;

    private Trans oTx;

    private AgreementABI oABIs;

    @Autowired
    Network network;

    @Autowired
    private ApplicationContext ctx;

    public Agreement() {
        oFSEthAddress        = new Account();
        oagreementAccountOil = new FSEthagreementAccount();

    }


    public Trans JoinAgreement(String iPrivateKey) throws FScx {

        FSNetGas oFSEthNetGas = new FSNetGas();

        oFSEthAddress.setEtherum(oNetwork.onetwork);

        SCTrans oFSTx = new FSTrans();

        oFSTx.createCallTrans(_oTxParams);
        oFSTx.submit(oNetwork.onetwork);

        return oFSTx.getTrans();

    }

}

AccountAgreement位于不同的包中,但它们都是用@ComponentScan扫描的,因此我不明白为什么我在 Account 类中的 Autowiring 有问题?

最佳答案

您没有使用 spring 来获取 Account 实例 - 所以 spring 没有机会 Autowiring 它。这一行:

oFSEthAddress        = new Account();

关于java - spring - 组件在一个类中 Autowiring ,但不在另一个类中 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293726/

相关文章:

java - 为什么生成的 PDF 在 Internet Explorer 中可以正常显示,但在 FireFox 或 Chrome 中却不能?

java - 请求作用域 bean 不会调用 @PreDestroy 方法

Spring-boot如何在被调用方法中使用 Autowiring 的类属性

java - 启动简单的非基于 Web 的 Java 应用程序的官方 Spring Boot 方式是什么?

java - 无法解析方法 'getLayoutInflater()' - Android Java

java - 如何在Eclipse自动生成的 'java'目录下创建 'src'目录?

spring - @ActiveProfile 和 spring.profiles.active

java - 使用@Autowired 将依赖项注入(inject)到使用 "new ..."创建的对象中

java - Bean Validation 验证一个验证是否有效?

java - 如何在运行时有条件地运行或启动 Spring 计划作业?