MentaContainer

Rev

Rev 100 | Rev 103 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 100 Rev 101
Line 16... Line 16...
16
                case4();
16
                case4();
17
                case5();
17
                case5();
18
                case6();
18
                case6();
19
                case7();
19
                case7();
20
                case8();
20
                case8();
-
 
21
                case9();
21
        }
22
        }
22
23
23
        public static class Connection {
24
        public static class Connection {
24
25
25
        }
26
        }
Line 68... Line 69...
68
                        assert conn != null;
69
                        assert conn != null;
69
70
70
                        // use the connection to get the balance...
71
                        // use the connection to get the balance...
71
                        return 1000000D;
72
                        return 1000000D;
72
                }
73
                }
-
 
74
        }
-
 
75
       
-
 
76
        private static void case9() {
-
 
77
               
-
 
78
                Container c = new MentaContainer();
-
 
79
-
 
80
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
-
 
81
                c.ioc("accountDAO", JdbcAccountDAO.class);
-
 
82
                c.ioc("userDAO", JdbcUserDAO.class);
-
 
83
-
 
84
                c.autowire("connection"); // all beans that need a connection in the constructor or setter will receive one...
-
 
85
-
 
86
                AccountDAO accountDAO = c.get("accountDAO");
-
 
87
                UserDAO userDAO = c.get("userDAO");
-
 
88
-
 
89
                System.out.println(accountDAO.getBalance(25)); // => 1000000
-
 
90
                System.out.println(userDAO.getUsername(45)); // => "saoj"
73
        }
91
        }
74
92
75
        private static void case1() {
93
        private static void case1() {
76
94
77
                Container c = new MentaContainer();
95
                Container c = new MentaContainer();
Line 116... Line 134...
116
134
117
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
135
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
118
136
119
                c.ioc("accountDAO", JdbcAccountDAO.class);
137
                c.ioc("accountDAO", JdbcAccountDAO.class);
120
138
121
                c.autowireByConstructor("connection"); // all beans that need a connection in the constructor will get one...
-
 
-
 
139
                c.autowire("connection"); // all beans that need a connection in the constructor will get one...
122
140
123
                AccountDAO accountDAO = c.get("accountDAO");
141
                AccountDAO accountDAO = c.get("accountDAO");
124
142
125
                System.out.println(accountDAO.getBalance(25)); // => 1000000
143
                System.out.println(accountDAO.getBalance(25)); // => 1000000
126
144
Line 145... Line 163...
145
163
146
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
164
                c.ioc("connection", Connection.class); // in real life this will be a connection pool factory...
147
165
148
                c.ioc("userDAO", JdbcUserDAO.class);
166
                c.ioc("userDAO", JdbcUserDAO.class);
149
167
150
                c.autowireBySetter("connection");
-
 
-
 
168
                c.autowire("connection");
151
169
152
                UserDAO userDAO = c.get("userDAO");
170
                UserDAO userDAO = c.get("userDAO");
153
171
154
                System.out.println(userDAO.getUsername(54)); // => "saoj"
172
                System.out.println(userDAO.getUsername(54)); // => "saoj"
155
173
Line 180... Line 198...
180
                                                       // or the hibernate SessionFactory
198
                                                       // or the hibernate SessionFactory
181
199
182
                // "conn" = the name of the property
200
                // "conn" = the name of the property
183
                // Connection.class = the type of the property
201
                // Connection.class = the type of the property
184
                // "connection" = the source from where the dependency will come from
202
                // "connection" = the source from where the dependency will come from
185
                c.autowireBySetter("connection");
-
 
-
 
203
                c.autowire("connection");
186
204
187
                UserDAO userDAO = c.get("userDAO");
205
                UserDAO userDAO = c.get("userDAO");
188
206
189
                // the container detects that userDAO has a dependency: name = "conn" and type = "Connection.class"
207
                // the container detects that userDAO has a dependency: name = "conn" and type = "Connection.class"
190
                // where does it go to get the dependency to insert?
208
                // where does it go to get the dependency to insert?
Line 212... Line 230...
212
230
213
                c.ioc("userDAO", JdbcUserDAO.class);
231
                c.ioc("userDAO", JdbcUserDAO.class);
214
232
215
                c.ioc("connection", Connection.class);
233
                c.ioc("connection", Connection.class);
216
234
217
                c.autowireBySetter("connection");
-
 
-
 
235
                c.autowire("connection");
218
236
219
                SomeService service = new SomeService();
237
                SomeService service = new SomeService();
220
238
221
                c.populate(service); // populate (inject) all properties of SomeService with
239
                c.populate(service); // populate (inject) all properties of SomeService with
222
                                     // beans from the container
240
                                     // beans from the container