[英]spring - component autowired in one class but not in another
我遇到一個奇怪的問題,一個類中提供了使用@Autowire
注入的Component
,而另一類中沒有。
我在Account
和Agreement
類的屬性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();
}
}
Account
和Agreement
類位於不同的程序包中,但都使用@ComponentScan
進行了掃描,因此我不明白為什么Account
類中的自動裝配有問題?
您沒有使用spring獲得Account實例-所以spring沒有機會自動裝配它。
oFSEthAddress = new Account();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.