MentaContainer

Rev

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