簡體   English   中英

彈簧-組件在一類中自動布線,但另一類中沒有

[英]spring - component autowired in one class but not in another

我遇到一個奇怪的問題,一個類中提供了使用@Autowire注入的Component ,而另一類中沒有。

我在AccountAgreement類的屬性network中使用@Autowired ,但僅在Agreement類而不在Account @ComponentScan運行在3個所需的軟件包上。

這是開始課程:

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();

    }
}

這是Component Network ,也必須在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不是自動接線的:

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類中的自動裝配有問題?

您沒有使用spring獲得Account實例-所以spring沒有機會自動裝配它。

oFSEthAddress        = new Account();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM