找不到Spring引导javabean

找不到Spring引导javabean,java,spring,spring-boot,javabeans,autowired,Java,Spring,Spring Boot,Javabeans,Autowired,我有90%使用服务和存储库访问的POJO类,但在这10%的情况下,我希望服务具有POJO属性,在这些情况下,自动连接失败 在运行时,我得到“com.ripple.trading.impl.Trader中构造函数的参数0需要找不到'com.ripple.repositories.pojo.Wallet'类型的bean”。这是Trader类: @Service public class Trader implements ITrade { private Wallet wallet;

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

在运行时,我得到“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;
}
}
这是我的主要应用程序

@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);
}
}
我猜是自动连接,可能会将应用程序分解成更小的部分,直到它再次工作。这是一个以前发生过的问题,重构修复了它,但是它背后的原因,为什么修复了它,它是正确的解决方案。这就是我现在想要的


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

您的
钱包
类不是由Spring管理的,它不是通过组件扫描可以获取的
@Component
,在您的配置中它没有定义为
@Bean


此外,Wallet类是有状态的,实际上您不应该让spring管理这个类,而是将它作为参数传递给所需的bean

您的
钱包
类不是由Spring管理的,它不是能够通过组件扫描获取的
@Component
,也不是在您的配置中定义为
@Bean


此外,Wallet类是有状态的,实际上您不应该让spring管理这个类,而是将它作为参数传递给所需的bean

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

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

这就是你收到这个错误的原因

如果您想将代码>贸易商>代码>作为“服务”,请考虑不要使用<代码>钱包> />代码>代码>建议>代码>这是字段,因为它本质上将状态引入到应该是无状态的状态中。如果您在贸易商>代码>中使用了一些方法,使用<代码>钱包和<代码>建议< /代码>考虑将这些参数作为参数传递给这些方法。比如:

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

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

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

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

这就是你收到这个错误的原因

如果您想将代码>贸易商>代码>作为“服务”,请考虑不要使用<代码>钱包> />代码>代码>建议>代码>这是字段,因为它本质上将状态引入到应该是无状态的状态中。如果您在贸易商>代码>中使用了一些方法,使用<代码>钱包和<代码>建议< /代码>考虑将这些参数作为参数传递给这些方法。比如:

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

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

SpringDataMongoDB参考文档说@document只是@Entity的一个自定义实现,或者说这完全不同。在构造函数中,我将其作为参数传递,然后
this.wallet=wallet一个
@Entity
不是Spring托管Bean的概念。哦,它是一个JPA注释,并且似乎没有必要,即使Spring Data mongodb参考文档说@document只是@Entity的一个自定义实现,或者说这不一样。在构造函数中,我将其作为参数传递,然后
this.wallet=wallet一个
@entity
不是Spring管理Bean的概念。哦,它是一个JPA注释,似乎没有必要,即使这是因为@Service使它成为一个单例,而单例不能保持状态?谢谢你的建议,现在我知道了这样做的细节
@Override public void trade(Wallet-Wallet,Advice-Advice){JSONObject order=null;Currency base=null;Currency counter=null;this.balance=WalletFetcher.getWalletByAddress(Wallet.getAddress());}
这确实需要一个“状态”下课。这是因为@Service使它成为一个单体,而单体不能保持状态吗?谢谢你的建议,现在我知道了这样做的细节
@Override public void trade(Wallet-Wallet,Advice-Advice){JSONObject order=null;Currency base=null;Currency counter=null;this.balance=WalletFetcher.getWalletByAddress(Wallet.getAddress());}
这确实使“状态”脱离了类。