95,10 → 95,27 |
|
} |
|
public static class SomeDependency { |
|
private String name; |
|
public SomeDependency() { } |
|
public void setName(String name) { |
this.name = name; |
} |
|
public String getName() { |
return name; |
} |
} |
|
public static class Connection { |
|
private final String name; |
|
private SomeDependency dep; |
|
public Connection(String name) { |
this.name = name; |
} |
108,8 → 125,14 |
} |
|
public String getName() { return name; } |
|
public void setMyDep(SomeDependency dep) { this.dep = dep; } |
|
public SomeDependency getMyDep() { return dep; } |
} |
|
|
|
public static class JdbcMyDAO implements MyDAO { |
|
private Connection conn; |
133,12 → 156,18 |
|
c.ioc("myDAO", JdbcMyDAO.class); |
|
c.ioc("aDependency", SomeDependency.class, true /* singleton */).addProperty("name", "A super dependency!"); |
|
c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
|
c.autowire("conn" /* the property name that will receive the dependency */, |
Connection.class /* the type of the property that will receive the dependency */, |
"connection" /* the source inside the container */); |
|
c.autowire("myDep" /* notice this is the setter of the Connection class */, |
SomeDependency.class /* the type - it could be an interface for better decoupling */, |
"aDependency" /* again this is the name of the bean/component inside the container */); |
|
MyDAO myDAO = (MyDAO) c.get("myDAO"); |
|
// check implementation... |
153,6 → 182,14 |
|
Assert.assertEquals("A super JDBC connection!", conn.getName()); |
|
// check dependency of dependency... |
|
Assert.assertEquals(c.get("aDependency"), conn.getMyDep()); |
|
Assert.assertTrue(c.get("aDependency") == conn.getMyDep()); // singleton! |
|
// check DAO can do its job... |
|
Assert.assertNotNull(myDAO.getSomething()); |
} |
} |