Exercise 12: Configuring Beans - jkneal/spring-angular-train GitHub Wiki

Goals

Learn how to configure beans using discovery and JavaConfig. In addition, learn how to fulfill bean dependencies using Autowiring

Instructions

  1. In edu.train.transaction, create a class named Bank. Make this a spring component (bean) using bean discovery.

  2. Add the following class fields with the specified values:

Field Type Value
name String "First National"
routingNumber String "183492239"
  1. Add the following method to the Bank class:
  public boolean transfer(BigDecimal amount) {
    return true;
  }
  1. In edu.train.SpringTrainApplication, add a bean named 'creditOnline'. This bean should be of type edu.train.transaction.CreditAuthorizer, and set the following values (note the values can be set using the constructor):
Field Value
name "CreditOnline"
authorizeEndpoint "creditonline.com/authorize"
  1. In edu.train.SpringTrainApplication, add a bean named 'jimsCreditCheck'. This bean should be of type edu.train.transaction.CreditAuthorizer, and set the following values:
Field Value
name "JimsCreditCheck"
authorizeEndpoint "jimscreditcheck.com/authorize"
  1. Now in edu.train.order, create an implementation of edu.train.order.OrderService named 'OrderServiceImpl'.

  2. Within OrderServiceImpl, inject the Bank and CreditAuthorizer beans you created earlier. Use Autowiring for getting the dependencies. For the CreditAuthorizer dependency, specify you want the bean with name 'creditOnline'.

  3. Within the same class, implement the method completeOrder with the following code:

  if (creditAuthorizer.isAuthorized(order.getCustomer().getName(), order.getCustomer().getCardNumber())) {
    return bank.transfer(order.getTotal());
  }
  return false;

VERIFICATION

  1. Start the training application and go to the app home page
  2. Click on the link 'Exercise Twelve'. Verify you get the result: "Order completed successfully!"