Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 3 ■ Applying Different Configurations @Before public void setup(){ context = new AnnotationConfigApplicationContext(MyDocumentsContext.class); engine = context.getBean(SearchEngine.class); webType = context.getBean(Type.class); } @Test public void testWithBeanConfigurationFindByType() { List documents = engine.findByType(webType); assertNotNull(documents); assertTrue(documents.size() == 1); assertEquals(webType.getName(),documents.get(0).getType().getName()); assertEquals(webType.getDesc(),documents.get(0).getType().getDesc()); assertEquals(webType.getExtension(),documents.get(0).getType().getExtension()); } } @Test public void testWithBeanConfigurationListAll() { List documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); } In Listing 3-12, you are now using the AnnotationConfigApplicationContext class to load your Java configuration class. This will tell the Spring container that the configuration will be based on a class and not on an XML file. Running your unit test (see Listing 3-12) will be the same as running the gradle command: gradle test Using the GroovyBeanDefinitionReader Class The new Spring Framework 4 introduces a new way to configure beans using the Groovy programming language. This new feature was based on the Grails Framework, introducing a DSL (domain-specific language) for creating beans. Let’s examine the following example in Listing 3-13. Listing 3-13. mydocuments.groovy import com.apress.isf.spring.service.ServiceSearchEngine import com.apress.isf.spring.data.DocumentRepository import com.apress.isf.java.model.Document beans { engine(ServiceSearchEngine){ documentDAO = ref("documentDAO") } 40
Chapter 3 ■ Applying Different Configurations documentDAO(DocumentRepository){ doc1 = ref("doc1") doc2 = ref("doc2") doc3 = ref("doc3") doc4 = ref("doc4") } doc1(Document){ name = "Book Template" type = ref("pdfType") location = "/Users/felipeg/Documents/Random/Book Template.pdf" } doc2(Document){ name = "Sample Contract" type = ref("pdfType") location = "/Users/felipeg/Documents/Contracts/Sample Contract.pdf" } doc3(Document){ name = "Clustering with RabbitMQ" type = ref("noteType") location = "/Users/felipeg/Documents/Random/Clustering with RabbitMQ.txt" } doc4(Document){ name = "Pro Spring Security Book" type = ref("webType") location = "http://www.apress.com/9781430248187" } webType(com.apress.isf.java.model.Type){ name = "WEB" desc = "Web Link" extension = ".url" } pdfType(com.apress.isf.java.model.Type){ name = "PDF" desc = "Portable Document Format" extension = ".url" } } noteType(com.apress.isf.java.model.Type){ name = "NOTE" desc = "Text Notes" extension = ".txt" } Listing 3-13 shows a new way to represent the XML (see Listing 3-6), the annotated (see Listing 3-9), or the Java configuration class (see Listing 3-10), but now using Groovy as a primary language. Right now you don’t need to worry about the syntax because I will be covering it in Chapter 18. 41
- 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 and 26: Chapter 2 ■ Working with Classes
- Page 27 and 28: 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: 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
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
Chapter 3 ■ Applying Different Configurations<br />
documentDAO(DocumentRepository){<br />
doc1 = ref("doc1")<br />
doc2 = ref("doc2")<br />
doc3 = ref("doc3")<br />
doc4 = ref("doc4")<br />
}<br />
doc1(Document){<br />
name = "Book Template"<br />
type = ref("pdfType")<br />
location = "/Users/felipeg/Documents/Random/Book Template.pdf"<br />
}<br />
doc2(Document){<br />
name = "Sample Contract"<br />
type = ref("pdfType")<br />
location = "/Users/felipeg/Documents/Contracts/Sample Contract.pdf"<br />
}<br />
doc3(Document){<br />
name = "Clustering with RabbitMQ"<br />
type = ref("noteType")<br />
location = "/Users/felipeg/Documents/Random/Clustering with RabbitMQ.txt"<br />
}<br />
doc4(Document){<br />
name = "Pro <strong>Spring</strong> Security Book"<br />
type = ref("webType")<br />
location = "http://www.apress.com/9781430248187"<br />
}<br />
webType(com.apress.isf.java.model.Type){<br />
name = "WEB"<br />
desc = "Web Link"<br />
extension = ".url"<br />
}<br />
pdfType(com.apress.isf.java.model.Type){<br />
name = "PDF"<br />
desc = "Portable Document Format"<br />
extension = ".url"<br />
}<br />
}<br />
noteType(com.apress.isf.java.model.Type){<br />
name = "NOTE"<br />
desc = "Text Notes"<br />
extension = ".txt"<br />
}<br />
Listing 3-13 shows a new way to represent the XML (see Listing 3-6), the annotated (see Listing 3-9), or the Java<br />
configuration class (see Listing 3-10), but now using Groovy as a primary language. Right now you don’t need to worry<br />
about the syntax because I will be covering it in Chapter 18.<br />
41