Java Reference
In-Depth Information
v . displayMessage ();
}
}
In this version, we have only three tasks:
1. Set up the Spring “context,” which provides the dependency injection framework.
2. Get the View from the context; it already has the Model set into it!
3. Ask the View to display some data.
Furthermore, we don't depend on particular implementations of the interface.
How does Spring know to “inject” or provide a Model to the View? And how does it know
what code to use for the View? There might be multiple implementations of the View inter-
face. Of course we have to tell it these things, which we'll do here with annotations:
Spring ConsoleViewer.java
@Component ( "myView" )
public
public class
class ConsoleViewer
ConsoleViewer implements
implements View {
Model messageProvider ;
@Override
public
public void
void displayMessage () {
System . out . println ( messageProvider . getMessage ());
}
@Resource ( name = "myModel" )
public
public void
void setModel ( Model messageProvider ) {
this
this . messageProvider = messageProvider ;
}
}
Note that whereas the @Component annotation is from Spring, the @Resource annotation is a
standard part of Java, in package javax.annotation .
Due to the persistence of information on the Web, if you do a web search for Spring Injec-
tion, you will probably find zillions of articles that refer to the older Spring 2.x way of doing
Search WWH ::




Custom Search