Rev 26 | Rev 28 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 26 | Rev 27 | ||
---|---|---|---|
Line 91... | Line 91... | ||
91 | 91 | ||
92 | public static interface MyDAO { |
92 | public static interface MyDAO { |
93 | 93 | ||
94 | public Object getSomething(); |
94 | public Object getSomething(); |
95 | 95 | ||
- | 96 | }
|
|
- | 97 | ||
- | 98 | public static class SomeDependency { |
|
- | 99 | ||
- | 100 | private String name; |
|
- | 101 | ||
- | 102 | public SomeDependency() { } |
|
- | 103 | ||
- | 104 | public void setName(String name) { |
|
- | 105 | this.name = name; |
|
- | 106 | }
|
|
- | 107 | ||
- | 108 | public String getName() { |
|
- | 109 | return name; |
|
- | 110 | }
|
|
96 | }
|
111 | }
|
97 | 112 | ||
98 | public static class Connection { |
113 | public static class Connection { |
99 | 114 | ||
100 | private final String name; |
115 | private final String name; |
- | 116 | ||
- | 117 | private SomeDependency dep; |
|
101 | 118 | ||
102 | public Connection(String name) { |
119 | public Connection(String name) { |
103 | this.name = name; |
120 | this.name = name; |
104 | }
|
121 | }
|
105 | 122 | ||
106 | public String toString() { |
123 | public String toString() { |
107 | return getClass().getName() + ": " + name; |
124 | return getClass().getName() + ": " + name; |
108 | }
|
125 | }
|
109 | 126 | ||
110 | public String getName() { return name; } |
127 | public String getName() { return name; } |
- | 128 | ||
- | 129 | public void setMyDep(SomeDependency dep) { this.dep = dep; } |
|
- | 130 | ||
- | 131 | public SomeDependency getMyDep() { return dep; } |
|
111 | }
|
132 | }
|
- | 133 | ||
- | 134 | ||
112 | 135 | ||
113 | public static class JdbcMyDAO implements MyDAO { |
136 | public static class JdbcMyDAO implements MyDAO { |
114 | 137 | ||
115 | private Connection conn; |
138 | private Connection conn; |
116 | 139 | ||
Line 130... | Line 153... | ||
130 | public void testAutoWiring() { |
153 | public void testAutoWiring() { |
131 | 154 | ||
132 | Container c = new MentaContainer(); |
155 | Container c = new MentaContainer(); |
133 | 156 | ||
134 | c.ioc("myDAO", JdbcMyDAO.class); |
157 | c.ioc("myDAO", JdbcMyDAO.class); |
- | 158 | ||
- | 159 | c.ioc("aDependency", SomeDependency.class, true /* singleton */).addProperty("name", "A super dependency!"); |
|
135 | 160 | ||
136 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
161 | c.ioc("connection", Connection.class).addInitValue("A super JDBC connection!"); |
137 | 162 | ||
138 | c.autowire("conn" /* the property name that will receive the dependency */, |
163 | c.autowire("conn" /* the property name that will receive the dependency */, |
139 | Connection.class /* the type of the property that will receive the dependency */, |
164 | Connection.class /* the type of the property that will receive the dependency */, |
140 | "connection" /* the source inside the container */); |
165 | "connection" /* the source inside the container */); |
- | 166 | ||
- | 167 | c.autowire("myDep" /* notice this is the setter of the Connection class */, |
|
- | 168 | SomeDependency.class /* the type - it could be an interface for better decoupling */, |
|
- | 169 | "aDependency" /* again this is the name of the bean/component inside the container */); |
|
141 | 170 | ||
142 | MyDAO myDAO = (MyDAO) c.get("myDAO"); |
171 | MyDAO myDAO = (MyDAO) c.get("myDAO"); |
143 | 172 | ||
144 | // check implementation...
|
173 | // check implementation...
|
145 | 174 | ||
Line 150... | Line 179... | ||
150 | Connection conn = ((JdbcMyDAO) myDAO).getConn(); |
179 | Connection conn = ((JdbcMyDAO) myDAO).getConn(); |
151 | 180 | ||
152 | Assert.assertNotNull(conn); |
181 | Assert.assertNotNull(conn); |
153 | 182 | ||
154 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
183 | Assert.assertEquals("A super JDBC connection!", conn.getName()); |
- | 184 | ||
- | 185 | // check dependency of dependency...
|
|
- | 186 | ||
- | 187 | Assert.assertEquals(c.get("aDependency"), conn.getMyDep()); |
|
- | 188 | ||
- | 189 | Assert.assertTrue(c.get("aDependency") == conn.getMyDep()); // singleton! |
|
- | 190 | ||
- | 191 | // check DAO can do its job...
|
|
155 | 192 | ||
156 | Assert.assertNotNull(myDAO.getSomething()); |
193 | Assert.assertNotNull(myDAO.getSomething()); |
157 | }
|
194 | }
|
158 | }
|
195 | }
|