MentaContainer

Rev

Rev 105 | 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;
57 soliveira 6
import org.mentacontainer.Scope;
39 soliveira 7
import org.mentacontainer.impl.MentaContainer;
8
 
9
public class BasicOperations {
100 soliveira 10
 
40 soliveira 11
        public static void main(String[] args) {
100 soliveira 12
 
39 soliveira 13
                case1();
14
                case2();
15
                case3();
16
                case4();
98 soliveira 17
                case5();
100 soliveira 18
                case6();
19
                case7();
20
                case8();
101 soliveira 21
                case9();
105 soliveira 22
                case10();
39 soliveira 23
        }
100 soliveira 24
 
25
        public static class Connection {
26
 
27
        }
28
 
98 soliveira 29
        public static interface UserDAO {
100 soliveira 30
 
98 soliveira 31
                public String getUsername(int id);
32
        }
100 soliveira 33
 
98 soliveira 34
        public static class JdbcUserDAO implements UserDAO {
100 soliveira 35
 
98 soliveira 36
                private Connection conn;
100 soliveira 37
 
38
                public void setConnection(Connection conn) {
39
                        this.conn = conn;
40
                }
41
 
98 soliveira 42
                @Override
43
                public String getUsername(int id) {
100 soliveira 44
 
98 soliveira 45
                        // connection will be injected by the container...
46
                        if (conn == null) throw new IllegalStateException("conn is null!");
100 soliveira 47
 
98 soliveira 48
                        // use the connection to get the username...
100 soliveira 49
 
98 soliveira 50
                        return "saoj";
51
                }
52
        }
100 soliveira 53
 
54
        public static interface AccountDAO {
55
 
56
                public double getBalance(int id);
57
        }
58
 
98 soliveira 59
        public static class JdbcAccountDAO implements AccountDAO {
100 soliveira 60
 
61
                private final Connection conn;
62
 
98 soliveira 63
                public JdbcAccountDAO(Connection conn) {
64
                        this.conn = conn;
65
                }
100 soliveira 66
 
98 soliveira 67
                @Override
68
                public double getBalance(int id) {
100 soliveira 69
 
70
                        assert conn != null;
71
 
98 soliveira 72
                        // use the connection to get the balance...
73
                        return 1000000D;
74
                }
75
        }
101 soliveira 76
 
77
        private static void case9() {
78
 
79
                Container c = new MentaContainer();
100 soliveira 80
 
101 soliveira 81
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
82
                c.ioc("accountDAO", JdbcAccountDAO.class);
83
                c.ioc("userDAO", JdbcUserDAO.class);
84
 
85
                c.autowire("connection"); // all beans that need a connection in the constructor or setter will receive one...
86
 
87
                AccountDAO accountDAO = c.get("accountDAO");
88
                UserDAO userDAO = c.get("userDAO");
89
 
90
                System.out.println(accountDAO.getBalance(25)); // => 1000000
91
                System.out.println(userDAO.getUsername(45)); // => "saoj"
92
        }
93
 
39 soliveira 94
        private static void case1() {
100 soliveira 95
 
39 soliveira 96
                Container c = new MentaContainer();
100 soliveira 97
 
39 soliveira 98
                c.ioc("myString1", String.class);
100 soliveira 99
 
41 soliveira 100
                String myString1 = c.get("myString1");
100 soliveira 101
 
39 soliveira 102
                System.out.println(myString1); // ==> "" ==> default constructor new String() was used
100 soliveira 103
 
39 soliveira 104
                c.ioc("myString2", String.class).addInitValue("saoj");
100 soliveira 105
 
41 soliveira 106
                String myString2 = c.get("myString2");
100 soliveira 107
 
39 soliveira 108
                System.out.println(myString2); // ==> "saoj" ==> constructor new String("saoj") was used
100 soliveira 109
 
91 soliveira 110
                c.ioc("myDate1", Date.class).addPropertyValue("hours", 15) // setHours(15)
100 soliveira 111
                        .addPropertyValue("minutes", 10) // setMinutes(10)
112
                        .addPropertyValue("seconds", 45); // setSeconds(45)
113
 
41 soliveira 114
                Date myDate1 = c.get("myDate1");
100 soliveira 115
 
39 soliveira 116
                System.out.println(myDate1); // ==> a date with time 15:10:45
117
        }
100 soliveira 118
 
98 soliveira 119
        private static void case5() {
100 soliveira 120
 
98 soliveira 121
                Container c = new MentaContainer();
100 soliveira 122
 
98 soliveira 123
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
100 soliveira 124
 
139 soliveira 125
                c.ioc("accountDAO", JdbcAccountDAO.class).addConstructorDependency("connection");
100 soliveira 126
 
98 soliveira 127
                AccountDAO accountDAO = c.get("accountDAO");
100 soliveira 128
 
129
                System.out.println(accountDAO.getBalance(25)); // => 1000000
98 soliveira 130
        }
100 soliveira 131
 
132
        private static void case7() {
133
 
134
                Container c = new MentaContainer();
135
 
136
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
137
 
138
                c.ioc("accountDAO", JdbcAccountDAO.class);
139
 
101 soliveira 140
                c.autowire("connection"); // all beans that need a connection in the constructor will get one...
100 soliveira 141
 
142
                AccountDAO accountDAO = c.get("accountDAO");
143
 
144
                System.out.println(accountDAO.getBalance(25)); // => 1000000
145
 
146
        }
147
 
148
        private static void case6() {
149
 
150
                Container c = new MentaContainer();
151
 
152
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
153
 
154
                c.ioc("userDAO", JdbcUserDAO.class).addPropertyDependency("connection");
155
 
156
                UserDAO userDAO = c.get("userDAO");
157
 
158
                System.out.println(userDAO.getUsername(54)); // => "saoj"
159
        }
160
 
161
        private static void case8() {
162
 
163
                Container c = new MentaContainer();
164
 
165
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
166
 
167
                c.ioc("userDAO", JdbcUserDAO.class);
168
 
101 soliveira 169
                c.autowire("connection");
100 soliveira 170
 
171
                UserDAO userDAO = c.get("userDAO");
172
 
173
                System.out.println(userDAO.getUsername(54)); // => "saoj"
174
 
175
        }
176
 
39 soliveira 177
        private static void case2() {
100 soliveira 178
 
39 soliveira 179
                Container c = new MentaContainer();
100 soliveira 180
 
57 soliveira 181
                c.ioc("myString", String.class, Scope.SINGLETON).addInitValue("saoj");
100 soliveira 182
 
41 soliveira 183
                String s1 = c.get("myString");
100 soliveira 184
 
41 soliveira 185
                String s2 = c.get("myString");
100 soliveira 186
 
39 soliveira 187
                System.out.println(s1 == s2); // ==> true ==> same instance
100 soliveira 188
 
39 soliveira 189
                System.out.println(s1.equals(s2)); // ==> true => of course
190
        }
100 soliveira 191
 
39 soliveira 192
        private static void case3() {
100 soliveira 193
 
39 soliveira 194
                Container c = new MentaContainer();
100 soliveira 195
 
39 soliveira 196
                c.ioc("userDAO", JdbcUserDAO.class);
100 soliveira 197
 
39 soliveira 198
                c.ioc("connection", Connection.class); // in real life this would be a connection pool
100 soliveira 199
                                                       // or the hibernate SessionFactory
200
 
39 soliveira 201
                // "conn" = the name of the property
202
                // Connection.class = the type of the property
203
                // "connection" = the source from where the dependency will come from
101 soliveira 204
                c.autowire("connection");
100 soliveira 205
 
41 soliveira 206
                UserDAO userDAO = c.get("userDAO");
100 soliveira 207
 
39 soliveira 208
                // the container detects that userDAO has a dependency: name = "conn" and type = "Connection.class"
209
                // where does it go to get the dependency to insert?
210
                // In itself: it does a Container.get("connection") => "connection" => the source
100 soliveira 211
 
39 soliveira 212
                System.out.println(userDAO.getUsername(11)); // ==> "saoj" ==> connection is not null as expected...
213
        }
100 soliveira 214
 
39 soliveira 215
        public static class SomeService {
100 soliveira 216
 
39 soliveira 217
                private UserDAO userDAO;
100 soliveira 218
 
39 soliveira 219
                public void setUserDAO(UserDAO userDAO) {
220
                        this.userDAO = userDAO;
221
                }
100 soliveira 222
 
39 soliveira 223
                public void doSomething() {
224
                        System.out.println(userDAO.getUsername(11));
225
                }
226
        }
100 soliveira 227
 
40 soliveira 228
        private static void case4() {
100 soliveira 229
 
39 soliveira 230
                Container c = new MentaContainer();
100 soliveira 231
 
39 soliveira 232
                c.ioc("userDAO", JdbcUserDAO.class);
100 soliveira 233
 
39 soliveira 234
                c.ioc("connection", Connection.class);
100 soliveira 235
 
101 soliveira 236
                c.autowire("connection");
100 soliveira 237
 
39 soliveira 238
                SomeService service = new SomeService();
100 soliveira 239
 
103 soliveira 240
                c.inject(service); // populate (inject) all properties of SomeService with
100 soliveira 241
                                     // beans from the container
242
 
39 soliveira 243
                service.doSomething(); // ==> "saoj"
244
        }
105 soliveira 245
 
246
        public static class SomeService2 {
100 soliveira 247
 
105 soliveira 248
                private final UserDAO userDAO;
249
 
250
                public SomeService2(UserDAO userDAO) {
251
                        this.userDAO = userDAO;
252
                }
253
 
254
                public void doSomething() {
255
                        System.out.println(userDAO.getUsername(11));
256
                }
257
        }
258
 
259
        private static void case10() {
260
 
261
                Container c = new MentaContainer();
262
 
263
                c.ioc("userDAO", JdbcUserDAO.class);
264
 
265
                c.ioc("connection", Connection.class);
266
 
267
                c.autowire("connection");
268
 
269
                SomeService2 service = c.construct(SomeService2.class);
270
 
271
                service.doSomething(); // ==> "saoj"
272
        }      
273
 
39 soliveira 274
}