java - 无法找到 Spring Boot javabean

标签 java spring spring-boot javabeans autowired

我有 90% 使用服务和存储库访问的 POJO 类,但在这 10% 的情况下,我希望服务具有 POJO 的属性,在这些情况下, Autowiring 会失败。

在运行时,我得到“com.ripple.trading.impl.Trader 中构造函数的参数 0 需要一个类型为‘com.ripple.repositories.pojo.Wallet’的 bean,但无法找到。”这是 Trader 类:

  @Service
public class Trader implements ITrade {
    private Wallet wallet;
    private List<Quantity> balance;
    private Advice latestAdvice;


@Autowired
private OrderService orderService;
@Autowired
private HistoryService historyService;
@Autowired
private CurrencyService currencyService;

private Advice lastAdvice;

public Trader(Wallet wallet, Advice advice){
    this.lastAdvice = Advice.HOLD;
    this.wallet = wallet;
    this.balance = WalletFetcher.getWalletByAddress(this.wallet.getAddress());
    this.latestAdvice = advice;
}
}

这是钱包

@Document
public class Wallet extends BaseModel implements Serializable{

private String address;
private String encryptedSecret;
private String name;


/**
 * Instantiates a new Wallet.
 *
 * @param address         the address
 * @param encryptedSecret the encrypted secret
 */
@PersistenceConstructor
public Wallet(String address, String encryptedSecret, String name) {
    this.address = address;
    this.encryptedSecret = encryptedSecret;
    this.name = name;
}

/**
 * Gets address.
 *
 * @return the address
 */
public String getAddress() {
    return address;
}

/**
 * Sets address.
 *
 * @param address the address
 */
public void setAddress(String address) {
    this.address = address;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

/**
 * Gets encrypted secret.
 *
 * @return the encrypted secret
 */
public String getEncryptedSecret() {
    return encryptedSecret;
}

/**
 * Sets encrypted secret.
 *
 * @param encryptedSecret the encrypted secret
 */
public void setEncryptedSecret(String encryptedSecret) {
    this.encryptedSecret = encryptedSecret;
}
}

这是我的主要应用程序

@EnableOAuth2Sso
@ComponentScan
@EnableMongoRepositories(value = "com.ripple.repositories.mongo")
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class RippleSpringApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(RippleSpringApplication.class);
}

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
public static void main(String[] args) {
    SpringApplication.run(RippleSpringApplication.class, args);
}
}

我的猜测是 Autowiring ,也许将应用程序分解成更小的部分,直到它再次工作。这是一个以前发生过的问题,重构解决了它,但其背后的原因,为什么要解决它,它是正确的解决方案吗?这就是我现在所追求的。

我的应用程序类位于根包中,因此 ComponentScan 不应该是问题。

最佳答案

Wallet不是Spring配置的bean。它只是一个类(一个 spring-data-mongodb 文档)。

如果您的 Spring 组件有一个构造函数,Spring 会自动考虑此构造函数进行 Autowiring ,并在调用该构造函数时查找与要提供的构造函数参数类型匹配的 bean。

这就是您收到该错误的原因。

如果您希望 Trader 成为一项“服务”,请考虑不要将 WalletAdvice 创建为字段,因为这本质上将状态引入到了应该做的事情中。无国籍。如果您在 Trader 中有对 WalletAdvice 执行某些操作的方法,请考虑将它们作为参数传递给这些方法。像这样的东西:

@Service
public class Trader {
  /// ... your autowired dependecies

  public BigDecimal getBalance(Wallet wallet) { 
    return WalletFetcher.getWalletByAddress(wallet.getAddress()); 
  }
}

关于java - 无法找到 Spring Boot javabean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44885805/

相关文章:

elasticsearch - 使用Spring Boot + Spring Data Elasticsearch的嵌套文档和父/子设置

Spring WebSocket 简单示例 - java.lang.IllegalStateException : Unexpected use of scheduler

Java httpServer 不同请求方式的基本认证

java - facescontext.getcurrentinstance 返回 nullpointerexception

java - 组织.hibernate.TransactionException : JDBC begin failed i

java - Spring 的 AspectJ NoSuchMethodError

java - cvc-elt.1.a : Cannot find the declaration of element "ear"

java - 单击按钮后如何在面板中显示信息?

java - JDBC - 结果集缓存管理

java - Swagger UI - 可为空值