MentaContainer

Compare Revisions

Ignore whitespace Rev 99 → Rev 100

/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java
7,175 → 7,221
import org.mentacontainer.impl.MentaContainer;
 
public class BasicOperations {
 
public static void main(String[] args) {
 
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
}
 
public static class Connection {
 
}
 
public static interface UserDAO {
 
public String getUsername(int id);
}
public static interface AccountDAO {
public double getBalance(int id);
}
public static class Connection {
}
 
public static class JdbcUserDAO implements UserDAO {
 
private Connection conn;
public void setConn(Connection conn) { this.conn = conn; }
 
public void setConnection(Connection conn) {
this.conn = conn;
}
 
@Override
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";
}
}
 
public static interface AccountDAO {
 
public double getBalance(int id);
}
 
public static class JdbcAccountDAO implements AccountDAO {
private Connection conn;
 
private final Connection conn;
 
public JdbcAccountDAO(Connection conn) {
this.conn = conn;
}
 
@Override
public double getBalance(int id) {
// connection will be injected by the container...
if (conn == null) throw new IllegalStateException("conn is null!");
 
assert conn != null;
 
// use the connection to get the balance...
return 1000000D;
}
}
 
private static void case1() {
 
Container c = new MentaContainer();
 
c.ioc("myString1", String.class);
 
String myString1 = c.get("myString1");
 
System.out.println(myString1); // ==> "" ==> default constructor new String() was used
 
c.ioc("myString2", String.class).addInitValue("saoj");
 
String myString2 = c.get("myString2");
 
System.out.println(myString2); // ==> "saoj" ==> constructor new String("saoj") was used
 
c.ioc("myDate1", Date.class).addPropertyValue("hours", 15) // setHours(15)
.addPropertyValue("minutes", 10) // setMinutes(10)
.addPropertyValue("seconds", 45); // setSeconds(45)
.addPropertyValue("minutes", 10) // setMinutes(10)
.addPropertyValue("seconds", 45); // setSeconds(45)
 
Date myDate1 = c.get("myDate1");
 
System.out.println(myDate1); // ==> a date with time 15:10:45
}
 
private static void case5() {
 
Container c = new MentaContainer();
 
c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
 
c.ioc("accountDAO", JdbcAccountDAO.class).addInitDependency("connection");
 
AccountDAO accountDAO = c.get("accountDAO");
System.out.println("Balance: " + accountDAO.getBalance(25));
 
System.out.println(accountDAO.getBalance(25)); // => 1000000
}
 
private static void case7() {
 
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.autowireByConstructor("connection"); // all beans that need a connection in the constructor will get one...
 
AccountDAO accountDAO = c.get("accountDAO");
 
System.out.println(accountDAO.getBalance(25)); // => 1000000
 
}
 
private static void case6() {
 
Container c = new MentaContainer();
 
c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
 
c.ioc("userDAO", JdbcUserDAO.class).addPropertyDependency("connection");
 
UserDAO userDAO = c.get("userDAO");
 
System.out.println(userDAO.getUsername(54)); // => "saoj"
}
 
private static void case8() {
 
Container c = new MentaContainer();
 
c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
 
c.ioc("userDAO", JdbcUserDAO.class);
 
c.autowireBySetter("connection");
 
UserDAO userDAO = c.get("userDAO");
 
System.out.println(userDAO.getUsername(54)); // => "saoj"
 
}
 
private static void case2() {
 
Container c = new MentaContainer();
 
c.ioc("myString", String.class, Scope.SINGLETON).addInitValue("saoj");
 
String s1 = c.get("myString");
 
String s2 = c.get("myString");
 
System.out.println(s1 == s2); // ==> true ==> same instance
 
System.out.println(s1.equals(s2)); // ==> true => of course
}
 
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
// 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.autowireBySetter("conn", "connection");
c.autowireBySetter("connection");
 
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() {
 
Container c = new MentaContainer();
 
c.ioc("userDAO", JdbcUserDAO.class);
 
c.ioc("connection", Connection.class);
c.autowireBySetter("conn", "connection");
 
c.autowireBySetter("connection");
 
SomeService service = new SomeService();
 
c.populate(service); // populate (inject) all properties of SomeService with
// beans from the container
// beans from the container
 
service.doSomething(); // ==> "saoj"
}
 
}