Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!