Rev 40 |
Rev 57 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
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
) {
case1
();
case2
();
case3
();
case4
();
}
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).
addProperty("hours",
15) // setHours(15)
.
addProperty("minutes",
10) // setMinutes(10)
.
addProperty("seconds",
45); // setSeconds(45)
Date myDate1 = 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 = 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
}
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 = 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.
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"
}
}