MentaContainer

Compare Revisions

Ignore whitespace Rev 40 → Rev 41

/trunk/src/main/java/org/mentacontainer/impl/GenericComponent.java
1,6 → 1,5
package org.mentacontainer.impl;
 
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
import org.mentacontainer.Component;
28,11 → 27,11
}
}
public Object getInstance() {
public <T> T getInstance() {
try {
return method.invoke(factory, (Object[]) null);
return (T) method.invoke(factory, (Object[]) null);
} catch(Exception e) {
/trunk/src/main/java/org/mentacontainer/impl/MentaComponent.java
194,9 → 194,9
return null;
}
public Object getInstance() {
public <T> T getInstance() {
if (singleton && singletonValue != null) return singletonValue;
if (singleton && singletonValue != null) return (T) singletonValue;
Object obj = null;
251,6 → 251,6
if (obj != null && singleton) singletonValue = obj;
return obj;
return (T) obj;
}
}
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
25,7 → 25,7
 
private Set<Dependency> dependencies = new HashSet<Dependency>();
 
public Object get(String key) {
public <T> T get(String key) {
 
if (!beans.containsKey(key)) throw new RuntimeException("Container does not have this bean set: " + key);
 
41,7 → 41,7
 
target = singletons.get(key);
 
return target;
return (T) target;
 
} else {
 
100,7 → 100,7
}
}
 
return target; // return target nicely with all the dependencies
return (T) target; // return target nicely with all the dependencies
 
} catch (Exception e) {
 
/trunk/src/main/java/org/mentacontainer/Component.java
13,7 → 13,7
*
* @return The instantiated bean based on the container configuration.
*/
public Object getInstance();
public <T> T getInstance();
/**
* Add a constructor parameter to be used when the bean is instantiated. It can be called more than once to
/trunk/src/main/java/org/mentacontainer/Container.java
27,7 → 27,7
* @param key The key representing the bean to return. The name of the bean in the container.
* @return The fully initialized and wired bean.
*/
public Object get(String key);
public <T> T get(String key);
/**
* Configure a bean to be returned with the given implementation when {@link #get(String)} is called.
/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java
21,13 → 21,13
c.ioc("myString1", String.class);
String myString1 = (String) c.get("myString1");
String myString1 = c.get("myString1");
System.out.println(myString1); // ==> "" ==> default constructor new String() was used
c.ioc("myString2", String.class).addInitValue("saoj");
String myString2 = (String) c.get("myString2");
String myString2 = c.get("myString2");
System.out.println(myString2); // ==> "saoj" ==> constructor new String("saoj") was used
35,7 → 35,7
.addProperty("minutes", 10) // setMinutes(10)
.addProperty("seconds", 45); // setSeconds(45)
Date myDate1 = (Date) c.get("myDate1");
Date myDate1 = c.get("myDate1");
System.out.println(myDate1); // ==> a date with time 15:10:45
}
46,9 → 46,9
c.ioc("myString", String.class, true /* singleton */).addInitValue("saoj");
String s1 = (String) c.get("myString");
String s1 = c.get("myString");
String s2 = (String) c.get("myString");
String s2 = c.get("myString");
System.out.println(s1 == s2); // ==> true ==> same instance
95,7 → 95,7
// "connection" = the source from where the dependency will come from
c.autowire("conn", Connection.class, "connection");
UserDAO userDAO = (UserDAO) c.get("userDAO");
UserDAO userDAO = c.get("userDAO");
// the container detects that userDAO has a dependency: name = "conn" and type = "Connection.class"
// where does it go to get the dependency to insert?