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.

<strong>Spring</strong> Stereotypes<br />

Chapter 3 ■ Applying Different Configurations<br />

<strong>Spring</strong> sterotypes are simply markers that help the <strong>Spring</strong> container to identify them as <strong>Spring</strong>-managed components.<br />

These markers can be used for external processing tools or can be used as a reference for pointcuts in an aspectoriented<br />

programming context. Nowadays in the <strong>Spring</strong> community, in many projects, the use of these stereotypes<br />

helps not only to understand the code by making it more readable but also by identifying the structure and the<br />

architectural layers. The most used stereotypes (by the <strong>Spring</strong> community and of course in your <strong>Spring</strong> application My<br />

Documents) are described in Table 3-2.<br />

Using the Java Bean Configuration<br />

The <strong>Spring</strong> <strong>Framework</strong> version 3.0 adopted a new way to configure the <strong>Spring</strong> container, using a new Java bean<br />

configuration class. This began as a separate project, but the <strong>Spring</strong> <strong>Framework</strong> team included it in version 3.0 as a<br />

part of its core. Nowadays it is one of the most preferable ways to configure the container, because it helps to get a<br />

clear picture of the relationship between classes and it shows how it interacts between them. And, in some cases it<br />

even helps to avoid all the clutter of using XML files.<br />

Now let’s see the following Java configuration class. Listing 3-11 is the equivalent of your XML configuration file<br />

(see Listing 3-6). Every bean definition in your XML corresponds to the @Bean annotation placed into the method.<br />

Listing 3-11. MyDocumentsContext.java<br />

package com.apress.isf.spring.config;<br />

import java.util.HashMap;<br />

import java.util.Map;<br />

import org.springframework.context.annotation.Bean;<br />

import org.springframework.context.annotation.Configuration;<br />

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

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.java.service.SearchEngine;<br />

import com.apress.isf.spring.data.DocumentDAO;<br />

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

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

@Configuration<br />

public class MyDocumentsContext {<br />

private Map documents = new HashMap();<br />

private Map types = new HashMap();<br />

@Bean<br />

public Type webType(){<br />

return getTypeFromMap("web");<br />

}<br />

@Bean<br />

public SearchEngine engine(){<br />

ServiceSearchEngine engine = new ServiceSearchEngine();<br />

engine.setDocumentDAO(documentDAO());<br />

return engine;<br />

}<br />

37

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

Saved successfully!

Ooh no, something went wrong!