MentaContainer

Compare Revisions

Ignore whitespace Rev 97 → Rev 98

/trunk/src/main/java/org/mentacontainer/example/BasicOperations.java
14,8 → 14,63
case2();
case3();
case4();
case5();
}
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; }
@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 class JdbcAccountDAO implements AccountDAO {
private 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!");
// use the connection to get the balance...
return 1000000D;
}
}
private static void case1() {
Container c = new MentaContainer();
41,6 → 96,19
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));
}
private static void case2() {
Container c = new MentaContainer();
56,33 → 124,6
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; }
@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";
}
}
private static void case3() {
Container c = new MentaContainer();