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 />

@Autowired<br />

private DocumentDAO documentDAO;<br />

public List findByType(Type documentType) {<br />

List result = new ArrayList();<br />

for(Document doc : listAll()){<br />

if(doc.getType().getName().equals(documentType.getName()))<br />

result.add(doc);<br />

}<br />

return result;<br />

}<br />

}<br />

public List listAll() {<br />

return Arrays.asList(documentDAO.getAll());<br />

}<br />

Listing 3-8 shows your new implementation of the SearchEngine, the AnnotatedSearchEngine class. As you can<br />

see, you are using the @Service(“engine”) and the @Autowired annotations. The <strong>Spring</strong> <strong>Framework</strong> supports several<br />

annotations (see Table 3-2) that are only markers or stereotypes like the @Service annotation. This annotation can<br />

have a value, in this case “engine”, meaning that the container will create a bean with id: "engine" and it will point to<br />

the AnnotatedSearchEngine class. This is the same as putting the following tag in an XML file:<br />

<br />

Table 3-2. Stereotypes<br />

Stereotype/Markers<br />

@Component<br />

@Repository<br />

@Service<br />

@Controller<br />

Description<br />

This is a marker, a generic stereotype that <strong>Spring</strong> will recognize as a <strong>Spring</strong>-managed<br />

component.<br />

This is a specialization of the @Component annotation and it fulfills the idea of the data access<br />

object. Classes annotated with this annotation can be processed by other tools or even<br />

aspects within the <strong>Spring</strong> container.<br />

This is a specialization of the @Component annotation and it fulfills the idea of a service layer.<br />

This is also a specialization of the @Component annotation and normally is used on a<br />

web context.<br />

Also, you used the @Autowired annotation. This annotation will tell the <strong>Spring</strong> container to actually create the<br />

instance and assign it to the declared variable. This will be the same as the following configuration:<br />

<br />

So, at the end, the AnnotatedSearchEngine class will be like having the following configuration all together:<br />

<br />

<br />

<br />

34

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

Saved successfully!

Ooh no, something went wrong!