MentaContainer

Compare Revisions

Ignore whitespace Rev 138 → Rev 139

/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
409,7 → 409,7
c.autowire("aDependency", "myDep");
c.ioc("someAction2", SomeAction2.class).addInitDependency("myDAO");
c.ioc("someAction2", SomeAction2.class).addConstructorDependency("myDAO");
SomeAction2 action = c.get("someAction2");
/trunk/src/test/java/org/mentacontainer/impl/ClassFactoryTest.java
66,7 → 66,7
c.ioc("myString", String.class).addInitValue("hello");
Factory c1 = new ClassFactory(c, TestObj1.class).addInitDependency("myString").addInitValue(20).addInitValue(30000L)
Factory c1 = new ClassFactory(c, TestObj1.class).addConstructorDependency("myString").addInitValue(20).addInitValue(30000L)
.addInitValue(new User("saoj"));
TestObj1 o = c1.getInstance();
/trunk/src/test/java/org/mentacontainer/example/CoreExamplesTest.java
62,7 → 62,7
c.ioc("username", String.class).addInitValue("saoj");
 
c.ioc("myString", String.class).addInitDependency("username");
c.ioc("myString", String.class).addConstructorDependency("username");
String myString = c.get("myString");
87,7 → 87,7
c.ioc("timeInMillis", Long.class).addInitValue(cal.getTimeInMillis());
c.ioc("myBirthdayMonth", Integer.class).addInitValue(0);
c.ioc("myBirthday", Date.class).addInitDependency("timeInMillis").addPropertyDependency("month", "myBirthdayMonth").addPropertyValue("date", 20);
c.ioc("myBirthday", Date.class).addConstructorDependency("timeInMillis").addPropertyDependency("month", "myBirthdayMonth").addPropertyValue("date", 20);
myBirthday = c.get("myBirthday");
161,7 → 161,7
c.ioc("timeInMillis2", Long.class).addInitValue(cal2.getTimeInMillis());
c.ioc("myBirthdayMonth", Integer.class).addInitValue(0);
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20).addInitDependency("timeInMillis2");
c.ioc("myBirthday", Date.class).addPropertyValue("date", 20).addConstructorDependency("timeInMillis2");
c.autowire("timeInMillis");
c.autowire("myBirthdayMonth", "month");
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
41,8 → 41,10
@Override
public Class<? extends Object> getType(Object key) {
Factory factory = factoriesByName.get(key.toString());
String k = InjectionUtils.getKeyName(key);
Factory factory = factoriesByName.get(k);
if (factory == null) return null;
return factory.getType();
115,7 → 117,7
@Override
public <T> T clear(Object key) {
String keyString = key.toString();
String keyString = InjectionUtils.getKeyName(key);
if (!factoriesByName.containsKey(keyString)) return null;
197,7 → 199,7
@Override
public <T> T get(Object key) {
String keyString = key.toString();
String keyString = InjectionUtils.getKeyName(key);
 
if (!factoriesByName.containsKey(keyString)) return null;
 
368,7 → 370,7
@Override
public Factory ioc(Object key, Factory factory, Scope scope) {
String keyString = key.toString();
String keyString = InjectionUtils.getKeyName(key);
factoriesByName.put(keyString, factory);
417,7 → 419,7
// autowire by constructor and setter...
String s = sourceFromContainer.toString();
String s = InjectionUtils.getKeyName(sourceFromContainer);
autowireBySetter(s);
429,7 → 431,7
// autowire by constructor and setter...
String s = sourceFromContainer.toString();
String s = InjectionUtils.getKeyName(sourceFromContainer);
autowireBySetter(beanProperty, s);
504,7 → 506,7
@Override
public synchronized boolean check(Object obj) {
String key = obj.toString();
String key = InjectionUtils.getKeyName(obj);
if (!factoriesByName.containsKey(key)) return false;
/trunk/src/main/java/org/mentacontainer/impl/ClassFactory.java
13,6 → 13,7
import org.mentacontainer.ConfigurableFactory;
import org.mentacontainer.util.FindConstructor;
import org.mentacontainer.util.FindMethod;
import org.mentacontainer.util.InjectionUtils;
 
/**
* The implementation of the Configurable Factory.
79,9 → 80,11
}
@Override
public ConfigurableFactory addPropertyDependency(String property, String key) {
public ConfigurableFactory addPropertyDependency(String property, Object key) {
return addPropertyValue(property, new DependencyKey(key));
String k = InjectionUtils.getKeyName(key);
return addPropertyValue(property, new DependencyKey(k));
}
@Override
91,9 → 94,11
}
@Override
public ConfigurableFactory addInitDependency(String key) {
public ConfigurableFactory addConstructorDependency(Object key) {
return addInitValue(new DependencyKey(key), container.getType(key));
String k = InjectionUtils.getKeyName(key);
return addInitValue(new DependencyKey(k), container.getType(k));
}
private ConfigurableFactory addInitValue(Object value, Class<? extends Object> type) {
/trunk/src/main/java/org/mentacontainer/ConfigurableFactory.java
45,7 → 45,7
* @param key The key used to get an instance from the container
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory addInitDependency(String key);
public ConfigurableFactory addConstructorDependency(Object key);
/**
* In case you want to force the use of a zero argument constructor and avoid any ambiguity when choosing the constructor to use.
85,5 → 85,5
* @param key The dependency name, in other words, the key used to get a bean from the container.
* @return The factory itself. (Fluent API)
*/
public ConfigurableFactory addPropertyDependency(String property, String key);
public ConfigurableFactory addPropertyDependency(String property, Object key);
}
/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java
122,7 → 122,7
 
c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
 
c.ioc("accountDAO", JdbcAccountDAO.class).addInitDependency("connection");
c.ioc("accountDAO", JdbcAccountDAO.class).addConstructorDependency("connection");
 
AccountDAO accountDAO = c.get("accountDAO");
 
/trunk/src/main/java/org/mentacontainer/util/InjectionUtils.java
354,6 → 354,20
}
return null;
}
public static String getKeyName(Object obj) {
if (obj instanceof Class<?>) {
Class<?> k = (Class<?>) obj;
String s = k.getSimpleName();
StringBuilder sb = new StringBuilder(s.length());
sb.append(s.substring(0, 1).toLowerCase());
if (s.length() > 1) {
sb.append(s.substring(1));
}
return sb.toString();
}
return obj.toString();
}
 
public static Method findMethodToGet(Class<? extends Object> target, String name) {