25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3 ■ Applying Different Configurations<br />

@Before<br />

public void setup(){<br />

context = new AnnotationConfigApplicationContext(MyDocumentsContext.class);<br />

engine = context.getBean(SearchEngine.class);<br />

webType = context.getBean(Type.class);<br />

}<br />

@Test<br />

public void testWithBeanConfigurationFindByType() {<br />

List documents = engine.findByType(webType);<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 1);<br />

assertEquals(webType.getName(),documents.get(0).getType().getName());<br />

assertEquals(webType.getDesc(),documents.get(0).getType().getDesc());<br />

assertEquals(webType.getExtension(),documents.get(0).getType().getExtension());<br />

}<br />

}<br />

@Test<br />

public void testWithBeanConfigurationListAll() {<br />

List documents = engine.listAll();<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 4);<br />

}<br />

In Listing 3-12, you are now using the AnnotationConfigApplicationContext class to load your Java configuration<br />

class. This will tell the <strong>Spring</strong> container that the configuration will be based on a class and not on an XML file.<br />

Running your unit test (see Listing 3-12) will be the same as running the gradle command:<br />

gradle test<br />

Using the GroovyBeanDefinitionReader Class<br />

The new <strong>Spring</strong> <strong>Framework</strong> 4 introduces a new way to configure beans using the Groovy programming language. This<br />

new feature was based on the Grails <strong>Framework</strong>, introducing a DSL (domain-specific language) for creating beans.<br />

Let’s examine the following example in Listing 3-13.<br />

Listing 3-13. mydocuments.groovy<br />

import com.apress.isf.spring.service.ServiceSearchEngine<br />

import com.apress.isf.spring.data.DocumentRepository<br />

import com.apress.isf.java.model.Document<br />

beans {<br />

engine(ServiceSearchEngine){<br />

documentDAO = ref("documentDAO")<br />

}<br />

40

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

Saved successfully!

Ooh no, something went wrong!