Hiya
How about this, that demonstrates both Dependency Lookup (DL) and Dependency Injection (DI)...
In beans.xml
Code:
<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="bingoService" class="foo.DefaultBingoService">
<constructor-arg index="0" ref="bingoRegistry"/>
</bean>
<bean id="bingoRegistry" class="foo.DefaultBingoRegistry"/>
</beans>
In Main.java
Code:
package foo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public final class Main {
public static void main(final String[] args) {
ApplicationContext ctx
= new FileSystemXmlApplicationContext("beans.xml");
IBingoService service = (IBingoService) ctx.getBean("bingoService");
service.registerPlayer(".NET Guy");
}
}
In IBingoService.java
Code:
package foo;
public interface IBingoService {
void registerPlayer(final String id);
}
In DefaultBingoService.java
Code:
package foo;
public final class DefaultBingoService implements IBingoService {
private final IBingoRegistry _registry;
public DefaultBingoService(final IBingoRegistry registry) {
_registry = registry;
}
public void registerPlayer(final String id) {
// add the player identified by the supplied id to the registry...
_registry.addPlayer(id);
}
}
In IBingoRegistry.java
Code:
package foo;
public interface IBingoRegistry {
void addPlayer(final String id);
}
In DefaultBingoRegistry.java
Code:
package foo;
public final class DefaultBingoRegistry implements IBingoRegistry {
public void addPlayer(final String id) {
System.out.println("Adding player '" + id + "'.");
}
}
Thats about as simple an application as one can get. It doesn't do anything other than demonstrate DL and DI, but there ya go. The IBingoRegistry dependency expressed by the DefaultBingoService class is injected into the 'bingoService' bean by the container. Note that the IBingoService interface itself has no such dependency on the IBingoRegistry class... the dependency is purely an implementation detail. The user (the Main.java program in this case) then uses the ApplicationContext as a glorified ServiceLocator to get an implementation of the IBingoService service bean.
Is that a simple enough example... or did you really want something a little more hardcore (i.e. something that doesn't use toy classes)?
Ciao
.NET Guy