/trunk/src/main/java/org/mentacontainer/impl/GenericComponent.java |
---|
10,14 → 10,10 |
private final Object factory; |
private final String name; |
private final Method method; |
public GenericComponent(String name, Object factory, String methodName) { |
public GenericComponent(Object factory, String methodName) { |
this.name = name; |
this.factory = factory; |
try { |
48,8 → 44,6 |
} |
} |
public String getName() { return name; } |
public Component addInitValue(Object value) { |
throw new UnsupportedOperationException(); |
/trunk/src/main/java/org/mentacontainer/impl/MentaComponent.java |
---|
18,8 → 18,6 |
*/ |
public class MentaComponent implements Component { |
private final String name; |
private final Class<? extends Object> klass; |
private Map<String, Object> props = null; |
34,18 → 32,16 |
private Object singletonValue = null; |
public MentaComponent(String name, Class<? extends Object> klass, boolean singleton) { |
public MentaComponent(Class<? extends Object> klass, boolean singleton) { |
this.name = name; |
this.klass = klass; |
this.singleton = singleton; |
} |
public MentaComponent(String name, Class<? extends Object> klass) { |
public MentaComponent(Class<? extends Object> klass) { |
this(name, klass, false); |
this(klass, false); |
} |
public boolean isSingleton() { return singleton; } |
119,28 → 115,6 |
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,10 → 108,8 |
} |
} |
public Component ioc(Component component) { |
public Component ioc(String key, 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); |
121,12 → 119,12 |
public Component ioc(String key, Class<? extends Object> klass) { |
return ioc(new MentaComponent(key, klass)); |
return ioc(key, new MentaComponent(klass)); |
} |
public Component ioc(String key, Class<? extends Object> klass, boolean singleton) { |
return ioc(new MentaComponent(key, klass, singleton)); |
return ioc(key, new MentaComponent(klass, singleton)); |
} |
public Dependency autowire(Dependency d) { |
/trunk/src/main/java/org/mentacontainer/Component.java |
---|
41,12 → 41,4 |
* @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,11 → 52,12 |
/** |
* 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(Component component); |
public Component ioc(String key, Component component); |
/** |
* Configure a bean dependency to be auto-wired by the container. In general you want the |
/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java |
---|
New file |
0,0 → 1,138 |
package org.mentacontainer.example; |
import java.util.Date; |
import org.mentacontainer.Container; |
import org.mentacontainer.impl.MentaContainer; |
public class BasicOperations { |
public static void main(String[] args) throws Exception { |
case1(); |
case2(); |
case3(); |
case4(); |
} |
private static void case1() { |
Container c = new MentaContainer(); |
c.ioc("myString1", String.class); |
String myString1 = (String) 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"); |
System.out.println(myString2); // ==> "saoj" ==> constructor new String("saoj") was used |
c.ioc("myDate1", Date.class).addProperty("hours", 15) // setHours(15) |
.addProperty("minutes", 10) // setMinutes(10) |
.addProperty("seconds", 45); // setSeconds(45) |
Date myDate1 = (Date) c.get("myDate1"); |
System.out.println(myDate1); // ==> a date with time 15:10:45 |
} |
private static void case2() { |
Container c = new MentaContainer(); |
c.ioc("myString", String.class, true /* singleton */).addInitValue("saoj"); |
String s1 = (String) c.get("myString"); |
String s2 = (String) c.get("myString"); |
System.out.println(s1 == s2); // ==> true ==> same instance |
System.out.println(s1.equals(s2)); // ==> true => of course |
} |
public static interface UserDAO { |
public String getUsername(int id); |
} |
public static class Connection { |
} |
public static class JdbcUserDAO implements UserDAO { |
private Connection conn; |
public void setConn(Connection conn) { this.conn = conn; } |
public String getUsername(int id) { |
// connection will be injected by the container... |
if (conn == null) throw new IllegalStateException("conn is null!"); |
// use the connection to get the username... |
return "saoj"; |
} |
} |
private static void case3() { |
Container c = new MentaContainer(); |
c.ioc("userDAO", JdbcUserDAO.class); |
c.ioc("connection", Connection.class); // in real life this would be a connection pool |
// or the hibernate SessionFactory |
// "conn" = the name of the property |
// Connection.class = the type of the property |
// "connection" = the source from where the dependency will come from |
c.autowire("conn", Connection.class, "connection"); |
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? |
// In itself: it does a Container.get("connection") => "connection" => the source |
System.out.println(userDAO.getUsername(11)); // ==> "saoj" ==> connection is not null as expected... |
} |
public static class SomeService { |
private UserDAO userDAO; |
public void setUserDAO(UserDAO userDAO) { |
this.userDAO = userDAO; |
} |
public void doSomething() { |
System.out.println(userDAO.getUsername(11)); |
} |
} |
private static void case4() throws Exception { |
Container c = new MentaContainer(); |
c.ioc("userDAO", JdbcUserDAO.class); |
c.ioc("connection", Connection.class); |
c.autowire("conn", Connection.class, "connection"); |
SomeService service = new SomeService(); |
c.populate(service); // populate (inject) all properties of SomeService with |
// beans from the container |
service.doSomething(); // ==> "saoj" |
} |
} |