spring - 组件在一个 class 中自动装配但在另一个中没有
spring - component autowired in one class but not in another
我 运行 遇到了一个奇怪的问题,其中注入 @Autowire
的 Component
在一个 class 中可用,但在另一个 class 中不可用。
我在 classes Account
和 Agreement
的属性 network
中使用 @Autowired
,但它仅在 [=49] 中自动装配=] 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();
}
}
这是 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); }
}
在此 class 中,字段 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()));
}
}
在此 class 中,字段 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();
}
}
class中的Account
和Agreement
在不同的包里,但是都是用@ComponentScan
扫描的,所以我不明白为什么会出现问题在 class Account
?
中使用自动装配
您没有使用 spring 获取帐户实例 - 因此 spring 没有机会自动连接 it.this 行:
oFSEthAddress = new Account();
我 运行 遇到了一个奇怪的问题,其中注入 @Autowire
的 Component
在一个 class 中可用,但在另一个 class 中不可用。
我在 classes Account
和 Agreement
的属性 network
中使用 @Autowired
,但它仅在 [=49] 中自动装配=] 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();
}
}
这是 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); }
}
在此 class 中,字段 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()));
}
}
在此 class 中,字段 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();
}
}
class中的Account
和Agreement
在不同的包里,但是都是用@ComponentScan
扫描的,所以我不明白为什么会出现问题在 class Account
?
您没有使用 spring 获取帐户实例 - 因此 spring 没有机会自动连接 it.this 行:
oFSEthAddress = new Account();