MentaContainer

Compare Revisions

Ignore whitespace Rev 100 → Rev 101

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
241,9 → 241,9
private Connection conn;
public void setConn(Connection conn) { this.conn = conn; }
public void setConnection(Connection conn) { this.conn = conn; }
public Connection getConn() { return conn; }
public Connection getConnection() { return conn; }
@Override
public Object getSomething() {
270,11 → 270,9
c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!");
c.autowireBySetter("conn" /* the property name that will receive the dependency */,
"connection" /* the source inside the container */);
c.autowire("connection");
c.autowireBySetter("myDep" /* notice this is the setter of the Connection class */,
"aDependency" /* again this is the name of the factory inside the container */);
c.autowire("aDependency", "myDep");
return c;
}
292,7 → 290,7
// check dependency...
Connection conn = ((JdbcMyDAO) myDAO).getConn();
Connection conn = ((JdbcMyDAO) myDAO).getConnection();
Assert.assertNotNull(conn);
336,7 → 334,7
// also check if myDAO was corrected wired...
Connection conn = ((JdbcMyDAO) a.getMyDAO()).getConn();
Connection conn = ((JdbcMyDAO) a.getMyDAO()).getConnection();
Assert.assertNotNull(conn);
/trunk/src/test/java/org/mentacontainer/example/CoreExamplesTest.java
106,7 → 106,7
 
c.ioc("myString", String.class);
c.autowireByConstructor("username");
c.autowire("username");
String myString = c.get("myString");
120,7 → 120,7
c.ioc("myBirthday", Date.class).addPropertyValue("month", 0).addPropertyValue("date", 20);
c.autowireBySetter("year", "myBirthdayYear");
c.autowire("myBirthdayYear", "year");
Date myBirthday = c.get("myBirthday");
139,8 → 139,8
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20);
c.autowireByConstructor("timeInMillis");
c.autowireBySetter("month", "myBirthdayMonth");
c.autowire("timeInMillis");
c.autowire("myBirthdayMonth", "month");
myBirthday = c.get("myBirthday");
163,8 → 163,8
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20).addInitDependency("timeInMillis2");
c.autowireByConstructor("timeInMillis");
c.autowireBySetter("month", "myBirthdayMonth");
c.autowire("timeInMillis");
c.autowire("myBirthdayMonth", "month");
myBirthday = c.get("myBirthday");
187,8 → 187,8
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20).useZeroArgumentConstructor();
c.autowireByConstructor("timeInMillis");
c.autowireBySetter("month", "myBirthdayMonth");
c.autowire("timeInMillis");
c.autowire("myBirthdayMonth", "month");
myBirthday = c.get("myBirthday");
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
421,10 → 421,27
return cc;
}
 
@Override
public void autowireBySetter(String targetProperty, String sourceFromContainer) {
public void autowire(String sourceFromContainer) {
// autowire by constructor and setter...
autowireBySetter(sourceFromContainer);
autowireByConstructor(sourceFromContainer);
}
@Override
public void autowire(String sourceFromContainer, String beanProperty) {
// only by setter, because constructors does not have parameter names or properties...
autowireBySetter(beanProperty, sourceFromContainer);
}
 
private void autowireBySetter(String targetProperty, String sourceFromContainer) {
Class<? extends Object> sourceType = getType(sourceFromContainer);
 
SetterDependency d = new SetterDependency(targetProperty, sourceFromContainer, sourceType);
432,14 → 449,12
setterDependencies.add(d);
}
@Override
public void autowireBySetter(String targetProperty) {
private void autowireBySetter(String targetProperty) {
autowireBySetter(targetProperty, targetProperty);
}
@Override
public void autowireByConstructor(String sourceFromContainer) {
private void autowireByConstructor(String sourceFromContainer) {
Class<? extends Object> sourceType = getType(sourceFromContainer);
/trunk/src/main/java/org/mentacontainer/Container.java
91,28 → 91,11
* @param source The dependency itself, coming from the container as well, in other words, the bean that will be injected in the original bean
* @return The container itself. (Fluent API)
*/
public void autowireBySetter(String property);
public void autowire(String sourceFromContainer);
/**
* Same as {@link #wire(String, Class, String)} except that it assumes that the property name will be the source name, in other words,
* the property name is the same as the bean name that will be injected as the dependency.
*
* @param property
* @param klass
* @return The container itself. (Fluent API)
*/
public void autowireBySetter(String property, String source);
public void autowire(String sourceFromContainer, String property);
/**
* Setup a dependency.
*
* @param dependency The dependency to setup
* @return The dependency itself. (Fluent API)
* @see Dependency
*/
public void autowireByConstructor(String source);
/**
* Take a given bean and populate its properties with other beans coming from this container. Perhaps you can call this auto-wiring.
* You basically checking properties of the given bean and looking for values (by name and type!) inside the container. And injecting
* in the given bean, in other words, populating it.
/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java
18,6 → 18,7
case6();
case7();
case8();
case9();
}
 
public static class Connection {
71,7 → 72,24
return 1000000D;
}
}
private static void case9() {
Container c = new MentaContainer();
 
c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
c.ioc("accountDAO", JdbcAccountDAO.class);
c.ioc("userDAO", JdbcUserDAO.class);
 
c.autowire("connection"); // all beans that need a connection in the constructor or setter will receive one...
 
AccountDAO accountDAO = c.get("accountDAO");
UserDAO userDAO = c.get("userDAO");
 
System.out.println(accountDAO.getBalance(25)); // => 1000000
System.out.println(userDAO.getUsername(45)); // => "saoj"
}
 
private static void case1() {
 
Container c = new MentaContainer();
118,7 → 136,7
 
c.ioc("accountDAO", JdbcAccountDAO.class);
 
c.autowireByConstructor("connection"); // all beans that need a connection in the constructor will get one...
c.autowire("connection"); // all beans that need a connection in the constructor will get one...
 
AccountDAO accountDAO = c.get("accountDAO");
 
147,7 → 165,7
 
c.ioc("userDAO", JdbcUserDAO.class);
 
c.autowireBySetter("connection");
c.autowire("connection");
 
UserDAO userDAO = c.get("userDAO");
 
182,7 → 200,7
// "conn" = the name of the property
// Connection.class = the type of the property
// "connection" = the source from where the dependency will come from
c.autowireBySetter("connection");
c.autowire("connection");
 
UserDAO userDAO = c.get("userDAO");
 
214,7 → 232,7
 
c.ioc("connection", Connection.class);
 
c.autowireBySetter("connection");
c.autowire("connection");
 
SomeService service = new SomeService();