Skip to content

Commit 0ae1897

Browse files
committed
GP-67 add AccountServiceDemoApp in order to show how ApplicationContext is created
1 parent dac1cc1 commit 0ae1897

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.config.ApplicationConfig;
4+
import com.bobocode.model.Account;
5+
import com.bobocode.service.AccountService;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
9+
/**
10+
* This is a demo application. It uses Spring to create an application context based on the configuration you provided
11+
* when implementing this exercise. When everything is done and application context created successfully,
12+
* it will get a "bean" (an object) of a {@link AccountService} from the context. That bean is used to find the richest
13+
* account and show that everything is working as expected.
14+
*/
15+
public class AccountServiceDemoApp {
16+
public static void main(String[] args) {
17+
ApplicationContext context = initializeSpringContext();
18+
AccountService accountService = context.getBean(AccountService.class);
19+
Account account = accountService.findRichestAccount();
20+
System.out.println("The riches person is " + account.getFirstName() + " " + account.getLastName());
21+
}
22+
23+
/**
24+
* In real applications, you will never initialize Spring context manually. We do this here for the sake of demo,
25+
* so you can see that it's an object that implements {@link ApplicationContext} interface. As you can see,
26+
* {@link ApplicationContext} has multiple implementations. In this exercise, we use the most popular way of
27+
* configuring context – Java config with annotations.
28+
*
29+
* @return an instance of application context
30+
*/
31+
private static ApplicationContext initializeSpringContext() {
32+
return new AnnotationConfigApplicationContext(ApplicationConfig.class);
33+
}
34+
}

0 commit comments

Comments
 (0)