2007, June 19 - 16:27 — laseelan
Dependency Injection
Within the web world, you won't find too many web applications that aren't built on top of the likes of Model View Controller, Business Delegate, Session Facade, Data Access Object, or other patterns these days. These patterns have been used to form architectures that attempt to provide a stronger foundation for our applications. By utilizing some of these patterns together, we avoid the problems faced by past development efforts, and provide extensibility for future growth. However, one problem still remains: component dependency resolution.
Take a look at the problem and learn how others have tried to solve the problem by utilizing frameworks that implement the Inversion of Control (or IoC) pattern
1) JDBCBridge.java
public class JDBCBridge {
private String type;
private String driverClass;
public JDBCBridge(String type,String driverClass) {
this.type=type;
this.driverClass=driverClass;
}
public JDBCBridge(){}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDriverClass() {
return driverClass;
}
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
}
2) drivers.xml
ODBC
org.techienet.bridge.ODBCBridge
OLEDB Bridge
org.techienet.bridge.OLEDBBridge
JDBC Bridge
org.techienet.bridge.JDBCBridge
3) BeanFactoryUsage.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class BeanFactoryUsage {
public static void main(String[] args) {
BeanFactory beans = new XmlBeanFactory(
new FileSystemResource("build/drivers.xml"));
JDBCBridge odbcBridge = (JDBCBridge) beans.getBean("odbc");
getInfo(odbcBridge);
JDBCBridge oledbBridge = (JDBCBridge) beans.getBean("oledb");
getInfo(oledbBridge );
JDBCBridge directBridge = (JDBCBridge) beans.getBean("direct");
getInfo(directBridge );
}
public static void getInfo(JDBCBridge bridge){
System.out.println(bridge.getType());
System.out.println(bridge.getDriverClass());
}
}