Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 2 ■ Working with Classes and Dependencies } @Test public void testWithSpringListAll() { List documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); } As you can see in Listing 2-8, you are using a ClassPathXmlApplicationContext class that will use your configuration file (see Listing 2-7); it will get the instance of the bean when you use the context.getBean method. The Spring Framework will automatically know what bean ID you are referring to because of its type. In this example, it will know that when you call context.getBean(SearchEngine.class) it will get your MySearchEngine implementation and so on for the Type class. Next, let’s run the test using the following command: gradle run –Dtest.single=MyDocumentsWithSpringTest test ■ ■Note Remember that you can run the gradle command from the project base directory and execute the command gradle run –Dtest.single=MyDocumentsWithSpringTest :ch02:test. The following output shows the result of running your unit test: :ch02:compileJava :ch02:compileGroovy :ch02:processResources UP-TO-DATE :ch02:classes :ch02:compileTestJava :ch02:compileTestGroovy :ch02:processTestResources :ch02:testClasses :ch02:test com.apress.isf.spring.test.MyDocumentsWithSpringTest > testWithSpringFindByType STARTED com.apress.isf.spring.test.MyDocumentsWithSpringTest > testWithSpringFindByType PASSED com.apress.isf.spring.test.MyDocumentsWithSpringTest > testWithSpringListAll STARTED com.apress.isf.spring.test.MyDocumentsWithSpringTest > testWithSpringListAll PASSED BUILD SUCCESSFUL Total time: 11.33 secs Spring Framework and Dependency Injection The Spring Framework offers many features, and one of the most used is the dependency injection pattern. The Spring Framework offers this feature as a container that helps to manage your classes though a life cycle that can be used by other objects (also called managed beans) and for other services (see Figure 2-3). 21
Chapter 2 ■ Working with Classes and Dependencies Figure 2-3. Dependency Injection Figure 2-3 shows how any implementation of your SearchEngine interface can be injected. Perhaps you are searching through a database or maybe you’re using a file to retrieve some information. The Spring Framework also provides several features such as the following: • Dependency injection • Aspect-oriented programming • Data access • Transaction management • Web flow and MVC • Social frameworks • Messaging • Remoting • Testing • and more . . . Right now you do not need to worry about each of these features. Over the course of the book, you are going to see each one of them in action through your Spring application, My Documents. I was talking about how the Spring Framework creates instances of your declared beans in the XML file (Listing 2-7), and how these are ready when you need them or are injected when required. But how does the Spring Framework know what classes need to be injected? The Spring Framework has several mechanisms to accomplish this: by name or by type. The injection can be either using Setters methods or constructors. But you are going to see this in detail in the following chapters. For now, let’s take a look at Figure 2-4 for an overview of the Spring container. 22
- Page 1 and 2: THE EXPERT’S VOICE ® IN SPRING
- Page 3 and 4: Contents at a Glance About the Auth
- Page 5 and 6: Introduction This book is an introd
- Page 7 and 8: Part I Spring Framework Basics The
- Page 9 and 10: Chapter 1 ■ Your First Spring App
- Page 11 and 12: Chapter 1 ■ Your First Spring App
- Page 13 and 14: Chapter 1 ■ Your First Spring App
- Page 15 and 16: Chapter 1 ■ Your First Spring App
- Page 17 and 18: Chapter 1 ■ Your First Spring App
- Page 19 and 20: Chapter 2 ■ Working with Classes
- Page 21 and 22: Chapter 2 ■ Working with Classes
- Page 23 and 24: Chapter 2 ■ Working with Classes
- Page 25: Chapter 2 ■ Working with Classes
- Page 29 and 30: Chapter 3 Applying Different Config
- Page 31 and 32: Chapter 3 ■ Applying Different Co
- Page 33 and 34: Chapter 3 ■ Applying Different Co
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
- Page 73 and 74: Chapter 6 ■ Using Resource Files
- Page 75 and 76: Chapter 6 ■ Using Resource Files
Chapter 2 ■ Working with Classes and Dependencies<br />
}<br />
@Test<br />
public void testWith<strong>Spring</strong>ListAll() {<br />
List documents = engine.listAll();<br />
assertNotNull(documents);<br />
assertTrue(documents.size() == 4);<br />
}<br />
As you can see in Listing 2-8, you are using a ClassPathXmlApplicationContext class that will use your<br />
configuration file (see Listing 2-7); it will get the instance of the bean when you use the context.getBean method.<br />
The <strong>Spring</strong> <strong>Framework</strong> will automatically know what bean ID you are referring to because of its type. In this<br />
example, it will know that when you call context.getBean(SearchEngine.class) it will get your MySearchEngine<br />
implementation and so on for the Type class.<br />
Next, let’s run the test using the following command:<br />
gradle run –Dtest.single=MyDocumentsWith<strong>Spring</strong>Test test<br />
■ ■Note Remember that you can run the gradle command from the project base directory and execute the command<br />
gradle run –Dtest.single=MyDocumentsWith<strong>Spring</strong>Test :ch02:test.<br />
The following output shows the result of running your unit test:<br />
:ch02:compileJava<br />
:ch02:compileGroovy<br />
:ch02:processResources UP-TO-DATE<br />
:ch02:classes<br />
:ch02:compileTestJava<br />
:ch02:compileTestGroovy<br />
:ch02:processTestResources<br />
:ch02:testClasses<br />
:ch02:test<br />
com.apress.isf.spring.test.MyDocumentsWith<strong>Spring</strong>Test > testWith<strong>Spring</strong>FindByType STARTED<br />
com.apress.isf.spring.test.MyDocumentsWith<strong>Spring</strong>Test > testWith<strong>Spring</strong>FindByType PASSED<br />
com.apress.isf.spring.test.MyDocumentsWith<strong>Spring</strong>Test > testWith<strong>Spring</strong>ListAll STARTED<br />
com.apress.isf.spring.test.MyDocumentsWith<strong>Spring</strong>Test > testWith<strong>Spring</strong>ListAll PASSED<br />
BUILD SUCCESSFUL<br />
Total time: 11.33 secs<br />
<strong>Spring</strong> <strong>Framework</strong> and Dependency Injection<br />
The <strong>Spring</strong> <strong>Framework</strong> offers many features, and one of the most used is the dependency injection pattern. The<br />
<strong>Spring</strong> <strong>Framework</strong> offers this feature as a container that helps to manage your classes though a life cycle that can be<br />
used by other objects (also called managed beans) and for other services (see Figure 2-3).<br />
21