【问题标题】:Spring boot javabean that could not be found找不到的Spring Boot javabean
【发布时间】:2017-07-03 12:37:51
【问题描述】:

我有 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;
}
}

这是钱包

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

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

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

【问题讨论】:

    标签: java spring spring-boot javabeans autowired


    【解决方案1】:

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

    如果你的 Spring 组件有一个构造函数,Spring 会自动考虑这个构造函数进行自动装配,并在调用该构造函数时寻找与构造函数参数类型匹配的 bean。

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

    如果您希望 Trader 成为“服务”,请考虑不要将 Wallet 和 Advice 设置为字段,因为这实际上将状态引入了应该是无状态的。如果您在Trader 中有与Wallet 和Advice 相关的方法,请考虑将它们作为参数传递给这些方法。比如:

    @Service
    public class Trader {
      /// ... your autowired dependecies
    
      public BigDecimal getBalance(Wallet wallet) { 
        return 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());} 这确实将“状态”带出课堂。
    【解决方案2】:

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

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

    【讨论】:

    • Spring Data mongodb 参考文档说@Docment 只是@Entity 的自定义实现,或者这只是不一样。在构造函数中,我将它作为参数传递,然后 this.wallet = wallet;
    • @Entitiy 不是 Spring Managed Bean 的概念。
    • 哦,这是一个 JPA 注释,甚至似乎没有必要
    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 2020-11-11
    • 2017-10-11
    • 2021-03-14
    • 2017-06-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多