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