MentaContainer

Rev

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