MentaContainer

Compare Revisions

Ignore whitespace Rev 39 → Rev 38

/trunk/src/test/java/org/mentacontainer/impl/GenericComponentTest.java
25,9 → 25,9
Container c = new MentaContainer();
// giveMeSomething => method that will be called to return object
Component generic = new GenericComponent(factory, "giveMeSomething");
Component generic = new GenericComponent("myFactory", factory, "giveMeSomething");
c.ioc("myFactory", generic);
c.ioc(generic);
String s1 = (String) c.get("myFactory");
/trunk/src/test/java/org/mentacontainer/impl/MentaComponentTest.java
29,9 → 29,9
@Test
public void testComponent() throws Exception {
Component c1 = new MentaComponent(User.class).addInitValue("saoj");
Component c1 = new MentaComponent("user1", User.class).addInitValue("saoj");
Component c2 = new MentaComponent(User.class).addProperty("username", "soliveira");
Component c2 = new MentaComponent("user2", User.class).addProperty("username", "soliveira");
User u1 = (User) c1.getInstance();
45,7 → 45,7
@Test
public void testSingleton() throws Exception {
Component c1 = new MentaComponent(User.class, true).addInitValue("saoj");
Component c1 = new MentaComponent("user", User.class, true).addInitValue("saoj");
User u1 = (User) c1.getInstance();
/trunk/src/main/java/org/mentacontainer/impl/GenericComponent.java
10,10 → 10,14
private final Object factory;
private final String name;
private final Method method;
public GenericComponent(Object factory, String methodName) {
public GenericComponent(String name, Object factory, String methodName) {
this.name = name;
this.factory = factory;
try {
44,6 → 48,8
}
}
public String getName() { return name; }
public Component addInitValue(Object value) {
throw new UnsupportedOperationException();
/trunk/src/main/java/org/mentacontainer/impl/MentaComponent.java
18,6 → 18,8
*/
public class MentaComponent implements Component {
private final String name;
private final Class<? extends Object> klass;
private Map<String, Object> props = null;
32,16 → 34,18
private Object singletonValue = null;
 
public MentaComponent(Class<? extends Object> klass, boolean singleton) {
public MentaComponent(String name, Class<? extends Object> klass, boolean singleton) {
this.name = name;
 
this.klass = klass;
this.singleton = singleton;
}
public MentaComponent(Class<? extends Object> klass) {
public MentaComponent(String name, Class<? extends Object> klass) {
this(klass, false);
this(name, klass, false);
}
public boolean isSingleton() { return singleton; }
115,6 → 119,28
return array;
}
public String getName() {
return name;
}
public boolean equals(Object o) {
if (o instanceof MentaComponent) {
MentaComponent c = (MentaComponent) o;
return c.name.equals(this.name);
}
return false;
}
public int hashCode() {
return name.hashCode();
}
/*
* Use reflection to set a property in the bean
*/
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java
108,8 → 108,10
}
}
 
public Component ioc(String key, Component component) {
public Component ioc(Component component) {
String key = component.getName();
 
if (beans.containsKey(key)) throw new RuntimeException("Container already set for the bean key: " + key);
 
beans.put(key, component);
119,12 → 121,12
public Component ioc(String key, Class<? extends Object> klass) {
return ioc(key, new MentaComponent(klass));
return ioc(new MentaComponent(key, klass));
}
public Component ioc(String key, Class<? extends Object> klass, boolean singleton) {
return ioc(key, new MentaComponent(klass, singleton));
return ioc(new MentaComponent(key, klass, singleton));
}
 
public Dependency autowire(Dependency d) {
/trunk/src/main/java/org/mentacontainer/Component.java
41,4 → 41,12
* @return true if it is a singleton component
*/
public boolean isSingleton();
/**
* What is the name of this component? The name is used when you request an instance from the container.
*
* @return The name of this component
*/
public String getName();
}
/trunk/src/main/java/org/mentacontainer/Container.java
52,12 → 52,11
/**
* Set up IoC based on the component passed.
*
* @param key The key representing the bean to return. The name of the bean in the container.
* @param component The component for the IoC.
* @return The component passed as a parameter. (Fluent API)
* @see Component
*/
public Component ioc(String key, Component component);
public Component ioc(Component component);
/**
* Configure a bean dependency to be auto-wired by the container. In general you want the