Spring 无法找到引导 javabean

Spring boot javabean that could not be found

我有 POJO classes,其中 90% 是使用服务和存储库访问的,但在这 10% 的情况下,我希望服务具有 POJO 的属性,在这些情况下,自动装配失败。

在运行时我得到"Parameter 0 of constructor in com.ripple.trading.impl.Trader required a bean of type 'com.ripple.repositories.pojo.Wallet' that could not be found."这是交易员class:

  @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);
}
}

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

我的应用程序class 在根包中,所以 ComponentScan 应该不是问题。

您的 wallet class 不受 Spring 管理,它不是 @Component 可以通过组件扫描获取,它没有定义为 @Bean 在您的配置中。

此外,钱包 class 是有状态的,实际上您不应该让 spring 管理这个 class,而是将其作为参数传递给所需的 Bean。

Class Wallet 不是 Spring 配置的 bean。它只是一个 class (一个 spring-data-mongodb document).

如果您的 Spring 组件只有一个构造函数,Spring 会自动将此构造函数视为自动装配,并会查找与构造函数参数类型匹配的 bean,以便在调用该构造函数时提供。

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

如果您希望 Trader 成为 "service",请考虑不要将 WalletAdvice 设为字段,因为这实质上将状态引入了无状态的状态。如果您在 Trader 中有对 WalletAdvice 执行某些操作的方法,请考虑将它们作为参数传递给这些方法。类似于:

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

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