public void setMessageProvider(MessageProvider provider) {
this.messageProvider = provider;
}
public MessageProvider getMessageProvider() {
return this.messageProvider;
}
}
package com.apress.prospring3.ch4;
public interface MessageProvider {
public String getMessage();
}
package com.apress.prospring3.ch4.xml;
import com.apress.prospring3.ch4.MessageProvider;
public class HelloWorldMessageProvider implements MessageProvider {
public String getMessage() {
return "Hello World!";
}
}
To declare the beans in XML file, on top of the basic configuration (as stated earlier in Listing 4-14),
you add the <bean> tags in Listing 4-18 to the file app-context-xml.xml.
Listing 4-18. Declare Spring Beans (XML)
<bean id="messageRenderer" class="com.apress.prospring3.ch4.xml.StandardOutMessageRenderer"/>
<bean id="messageProvider" class="com.apress.prospring3.ch4.xml.HelloWorldMessageProvider"/>
The previous tags declare two beans, one with an ID of "messageProvider" with the
HelloWorldMessageProvider implementation and the other with an ID of "messageRenderer" with the
StandardOutMessageRenderer implementation.
To define the Spring beans via annotation, you don't need to modify the XML configuration file
(app-context-annotation.xml) anymore; you just need to add the corresponding annotation to the service
implementation classes under the package com.apress.prospring3.ch4.annotation (see Listing 4-19).
Listing 4-19. Declare Spring Beans (Annotation)
package com.apress.prospring3.ch4.annotation;
import org.springframework.stereotype.Service;
// Rest of codes omitted
@Service("messageRenderer")
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home