Rev 41 | Rev 76 | 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 | |||
35 | c.ioc("myDate1", Date.class).addProperty("hours", 15) // setHours(15) |
||
36 | .addProperty("minutes", 10) // setMinutes(10) |
||
37 | .addProperty("seconds", 45); // setSeconds(45) |
||
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 | |||
74 | public String getUsername(int id) { |
||
75 | |||
76 | // connection will be injected by the container... |
||
77 | if (conn == null) throw new IllegalStateException("conn is null!"); |
||
78 | |||
79 | // use the connection to get the username... |
||
80 | |||
81 | return "saoj"; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | private static void case3() { |
||
86 | |||
87 | Container c = new MentaContainer(); |
||
88 | |||
89 | c.ioc("userDAO", JdbcUserDAO.class); |
||
90 | |||
91 | c.ioc("connection", Connection.class); // in real life this would be a connection pool |
||
92 | // or the hibernate SessionFactory |
||
93 | |||
94 | // "conn" = the name of the property |
||
95 | // Connection.class = the type of the property |
||
96 | // "connection" = the source from where the dependency will come from |
||
97 | c.autowire("conn", Connection.class, "connection"); |
||
98 | |||
41 | soliveira | 99 | UserDAO userDAO = c.get("userDAO"); |
39 | soliveira | 100 | |
101 | // the container detects that userDAO has a dependency: name = "conn" and type = "Connection.class" |
||
102 | // where does it go to get the dependency to insert? |
||
103 | // In itself: it does a Container.get("connection") => "connection" => the source |
||
104 | |||
105 | System.out.println(userDAO.getUsername(11)); // ==> "saoj" ==> connection is not null as expected... |
||
106 | } |
||
107 | |||
108 | public static class SomeService { |
||
109 | |||
110 | private UserDAO userDAO; |
||
111 | |||
112 | public void setUserDAO(UserDAO userDAO) { |
||
113 | this.userDAO = userDAO; |
||
114 | } |
||
115 | |||
116 | public void doSomething() { |
||
117 | System.out.println(userDAO.getUsername(11)); |
||
118 | } |
||
119 | } |
||
120 | |||
40 | soliveira | 121 | private static void case4() { |
39 | soliveira | 122 | |
123 | Container c = new MentaContainer(); |
||
124 | |||
125 | c.ioc("userDAO", JdbcUserDAO.class); |
||
126 | |||
127 | c.ioc("connection", Connection.class); |
||
128 | |||
129 | c.autowire("conn", Connection.class, "connection"); |
||
130 | |||
131 | SomeService service = new SomeService(); |
||
132 | |||
133 | c.populate(service); // populate (inject) all properties of SomeService with |
||
134 | // beans from the container |
||
135 | |||
136 | service.doSomething(); // ==> "saoj" |
||
137 | } |
||
138 | |||
139 | } |