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
-
In
edu.train.transaction, create a class namedBank. Make this a spring component (bean) using bean discovery. -
Add the following class fields with the specified values:
| Field | Type | Value |
|---|---|---|
| name | String | "First National" |
| routingNumber | String | "183492239" |
- Add the following method to the Bank class:
public boolean transfer(BigDecimal amount) {
return true;
}
- In
edu.train.SpringTrainApplication, add a bean named 'creditOnline'. This bean should be of typeedu.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" |
- In
edu.train.SpringTrainApplication, add a bean named 'jimsCreditCheck'. This bean should be of typeedu.train.transaction.CreditAuthorizer, and set the following values:
| Field | Value |
|---|---|
| name | "JimsCreditCheck" |
| authorizeEndpoint | "jimscreditcheck.com/authorize" |
-
Now in
edu.train.order, create an implementation ofedu.train.order.OrderServicenamed 'OrderServiceImpl'. -
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'.
-
Within the same class, implement the method
completeOrderwith the following code:
if (creditAuthorizer.isAuthorized(order.getCustomer().getName(), order.getCustomer().getCardNumber())) {
return bank.transfer(order.getTotal());
}
return false;
VERIFICATION
- Start the training application and go to the app home page
- Click on the link 'Exercise Twelve'. Verify you get the result: "Order completed successfully!"