Rev 40 | Rev 57 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
39 | soliveira | 1 | package org.mentacontainer.example; |
2 | |||
3 | import java.util.Date; |
||
4 | |||
5 | import org.mentacontainer.Container; |
||
6 | import org.mentacontainer.impl.MentaContainer; |
||
7 | |||
8 | public class BasicOperations { |
||
9 | |||
40 | soliveira | 10 | public static void main(String[] args) { |
39 | soliveira | 11 | |
12 | case1(); |
||
13 | case2(); |
||
14 | case3(); |
||
15 | case4(); |
||
16 | } |
||
17 | |||
18 | private static void case1() { |
||
19 | |||
20 | Container c = new MentaContainer(); |
||
21 | |||
22 | c.ioc("myString1", String.class); |
||
23 | |||
41 | soliveira | 24 | String myString1 = c.get("myString1"); |
39 | soliveira | 25 | |
26 | System.out.println(myString1); // ==> "" ==> default constructor new String() was used |
||
27 | |||
28 | c.ioc("myString2", String.class).addInitValue("saoj"); |
||
29 | |||
41 | soliveira | 30 | String myString2 = c.get("myString2"); |
39 | soliveira | 31 | |
32 | System.out.println(myString2); // ==> "saoj" ==> constructor new String("saoj") was used |
||
33 | |||
34 | c.ioc("myDate1", Date.class).addProperty("hours", 15) // setHours(15) |
||
35 | .addProperty("minutes", 10) // setMinutes(10) |
||
36 | .addProperty("seconds", 45); // setSeconds(45) |
||
37 | |||
41 | soliveira | 38 | Date myDate1 = c.get("myDate1"); |
39 | soliveira | 39 | |
40 | System.out.println(myDate1); // ==> a date with time 15:10:45 |
||
41 | } |
||
42 | |||
43 | private static void case2() { |
||
44 | |||
45 | Container c = new MentaContainer(); |
||
46 | |||
47 | c.ioc("myString", String.class, true /* singleton */).addInitValue("saoj"); |
||
48 | |||
41 | soliveira | 49 | String s1 = c.get("myString"); |
39 | soliveira | 50 | |
41 | soliveira | 51 | String s2 = c.get("myString"); |
39 | soliveira | 52 | |
53 | System.out.println(s1 == s2); // ==> true ==> same instance |
||
54 | |||
55 | System.out.println(s1.equals(s2)); // ==> true => of course |
||
56 | } |
||
57 | |||
58 | public static interface UserDAO { |
||
59 | |||
60 | public String getUsername(int id); |
||
61 | } |
||
62 | |||
63 | public static class Connection { |
||
64 | |||
65 | } |
||
66 | |||
67 | public static class JdbcUserDAO implements UserDAO { |
||
68 | |||
69 | private Connection conn; |
||
70 | |||
71 | public void setConn(Connection conn) { this.conn = conn; } |
||
72 | |||
73 | public String getUsername(int id) { |
||
74 | |||
75 | // connection will be injected by the container... |
||
76 | if (conn == null) throw new IllegalStateException("conn is null!"); |
||
77 | |||
78 | // use the connection to get the username... |
||
79 | |||
80 | return "saoj"; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | private static void case3() { |
||
85 | |||
86 | Container c = new MentaContainer(); |
||
87 | |||
88 | c.ioc("userDAO", JdbcUserDAO.class); |
||
89 | |||
90 | c.ioc("connection", Connection.class); // in real life this would be a connection pool |
||
91 | // or the hibernate SessionFactory |
||
92 | |||
93 | // "conn" = the name of the property |
||
94 | // Connection.class = the type of the property |
||
95 | // "connection" = the source from where the dependency will come from |
||
96 | c.autowire("conn", Connection.class, "connection"); |
||
97 | |||
41 | soliveira | 98 | UserDAO userDAO = c.get("userDAO"); |
39 | soliveira | 99 | |
100 | // the container detects that userDAO has a dependency: name = "conn" and type = "Connection.class" |
||
101 | // where does it go to get the dependency to insert? |
||
102 | // In itself: it does a Container.get("connection") => "connection" => the source |
||
103 | |||
104 | System.out.println(userDAO.getUsername(11)); // ==> "saoj" ==> connection is not null as expected... |
||
105 | } |
||
106 | |||
107 | public static class SomeService { |
||
108 | |||
109 | private UserDAO userDAO; |
||
110 | |||
111 | public void setUserDAO(UserDAO userDAO) { |
||
112 | this.userDAO = userDAO; |
||
113 | } |
||
114 | |||
115 | public void doSomething() { |
||
116 | System.out.println(userDAO.getUsername(11)); |
||
117 | } |
||
118 | } |
||
119 | |||
40 | soliveira | 120 | private static void case4() { |
39 | soliveira | 121 | |
122 | Container c = new MentaContainer(); |
||
123 | |||
124 | c.ioc("userDAO", JdbcUserDAO.class); |
||
125 | |||
126 | c.ioc("connection", Connection.class); |
||
127 | |||
128 | c.autowire("conn", Connection.class, "connection"); |
||
129 | |||
130 | SomeService service = new SomeService(); |
||
131 | |||
132 | c.populate(service); // populate (inject) all properties of SomeService with |
||
133 | // beans from the container |
||
134 | |||
135 | service.doSomething(); // ==> "saoj" |
||
136 | } |
||
137 | |||
138 | } |