0
# Bean Management & Dependency Injection
1
2
Spring-based dependency injection system providing comprehensive bean location, property access, manipulation, and Velocity template integration for the Liferay portal framework.
3
4
## Capabilities
5
6
### Bean Location
7
8
Locates and retrieves beans from the Spring ApplicationContext with special handling for Velocity template integration.
9
10
```java { .api }
11
/**
12
* Implementation of BeanLocator interface providing Spring ApplicationContext-based bean location
13
*/
14
public class BeanLocatorImpl implements BeanLocator {
15
16
/**
17
* Locates a bean by name with special handling for .velocity suffix
18
* @param name the bean name to locate
19
* @return the located bean instance
20
* @throws BeanLocatorException if bean not found
21
*/
22
public Object locate(String name) throws BeanLocatorException;
23
24
/**
25
* Locates beans by type and returns a map of bean names to instances
26
* @param clazz the class type to locate
27
* @return Map of bean names to bean instances of specified type
28
* @throws BeanLocatorException if beans cannot be located
29
*/
30
public <T> Map<String, T> locate(Class<T> clazz) throws BeanLocatorException;
31
32
/**
33
* Returns all bean definition names in the ApplicationContext
34
* @return array of all bean names
35
*/
36
public String[] getNames();
37
38
/**
39
* Cleanup method for ApplicationContext resources
40
*/
41
public void destroy();
42
43
/**
44
* Gets the underlying Spring ApplicationContext
45
* @return ApplicationContext instance
46
*/
47
public ApplicationContext getApplicationContext();
48
49
/**
50
* Gets the ClassLoader used by this bean locator
51
* @return ClassLoader instance
52
*/
53
public ClassLoader getClassLoader();
54
55
/**
56
* Gets the type of the specified bean
57
* @param name the bean name
58
* @return Class type of the bean
59
*/
60
public Class<?> getType(String name);
61
}
62
```
63
64
**Usage Examples:**
65
66
```java
67
BeanLocatorImpl beanLocator = new BeanLocatorImpl();
68
69
// Locate bean by name
70
Object myService = beanLocator.locate("myServiceBean");
71
72
// Locate beans by type (returns Map)
73
Map<String, UserService> userServices = beanLocator.locate(UserService.class);
74
UserService userService = userServices.values().iterator().next();
75
76
// Get all bean names
77
String[] allBeans = beanLocator.getNames();
78
79
// Special Velocity integration - appending .velocity suffix
80
Object velocityBean = beanLocator.locate("templateEngine.velocity");
81
```
82
83
### Bean Properties
84
85
Comprehensive bean property access and manipulation utilities with support for all primitive types, HTTP parameter binding, and deep copying.
86
87
```java { .api }
88
/**
89
* Implementation of BeanProperties interface for bean property operations
90
*/
91
public class BeanPropertiesImpl implements BeanProperties {
92
93
/**
94
* Get boolean property value from bean
95
* @param bean the target bean
96
* @param param the property name
97
* @return boolean value of property
98
*/
99
public boolean getBoolean(Object bean, String param);
100
101
/**
102
* Get boolean property value from bean with default value
103
* @param bean the target bean
104
* @param param the property name
105
* @param defaultValue the default value if property not found
106
* @return boolean value of property or default value
107
*/
108
public boolean getBoolean(Object bean, String param, boolean defaultValue);
109
110
/**
111
* Get boolean property value from bean without throwing exceptions
112
* @param bean the target bean
113
* @param param the property name
114
* @return boolean value or false if property not found
115
*/
116
public boolean getBooleanSilent(Object bean, String param);
117
118
/**
119
* Get boolean property value from bean without throwing exceptions with default value
120
* @param bean the target bean
121
* @param param the property name
122
* @param defaultValue the default value if property not found
123
* @return boolean value or default value if property not found
124
*/
125
public boolean getBooleanSilent(Object bean, String param, boolean defaultValue);
126
127
/**
128
* Get integer property value from bean
129
* @param bean the target bean
130
* @param param the property name
131
* @return integer value of property
132
*/
133
public int getInteger(Object bean, String param);
134
135
/**
136
* Get integer property value from bean with default value
137
* @param bean the target bean
138
* @param param the property name
139
* @param defaultValue the default value if property not found
140
* @return integer value of property or default value
141
*/
142
public int getInteger(Object bean, String param, int defaultValue);
143
144
/**
145
* Get integer property value from bean without throwing exceptions
146
* @param bean the target bean
147
* @param param the property name
148
* @return integer value or 0 if property not found
149
*/
150
public int getIntegerSilent(Object bean, String param);
151
152
/**
153
* Get integer property value from bean without throwing exceptions with default value
154
* @param bean the target bean
155
* @param param the property name
156
* @param defaultValue the default value if property not found
157
* @return integer value or default value if property not found
158
*/
159
public int getIntegerSilent(Object bean, String param, int defaultValue);
160
161
/**
162
* Get long property value from bean
163
* @param bean the target bean
164
* @param param the property name
165
* @return long value of property
166
*/
167
public long getLong(Object bean, String param);
168
169
/**
170
* Get long property value from bean with default value
171
* @param bean the target bean
172
* @param param the property name
173
* @param defaultValue the default value if property not found
174
* @return long value of property or default value
175
*/
176
public long getLong(Object bean, String param, long defaultValue);
177
178
/**
179
* Get long property value from bean without throwing exceptions
180
* @param bean the target bean
181
* @param param the property name
182
* @return long value or 0 if property not found
183
*/
184
public long getLongSilent(Object bean, String param);
185
186
/**
187
* Get long property value from bean without throwing exceptions with default value
188
* @param bean the target bean
189
* @param param the property name
190
* @param defaultValue the default value if property not found
191
* @return long value or default value if property not found
192
*/
193
public long getLongSilent(Object bean, String param, long defaultValue);
194
195
/**
196
* Get byte property value from bean
197
* @param bean the target bean
198
* @param param the property name
199
* @return byte value of property
200
*/
201
public byte getByte(Object bean, String param);
202
203
/**
204
* Get byte property value from bean with default value
205
* @param bean the target bean
206
* @param param the property name
207
* @param defaultValue the default value if property not found
208
* @return byte value of property or default value
209
*/
210
public byte getByte(Object bean, String param, byte defaultValue);
211
212
/**
213
* Get byte property value from bean without throwing exceptions
214
* @param bean the target bean
215
* @param param the property name
216
* @return byte value or 0 if property not found
217
*/
218
public byte getByteSilent(Object bean, String param);
219
220
/**
221
* Get byte property value from bean without throwing exceptions with default value
222
* @param bean the target bean
223
* @param param the property name
224
* @param defaultValue the default value if property not found
225
* @return byte value or default value if property not found
226
*/
227
public byte getByteSilent(Object bean, String param, byte defaultValue);
228
229
/**
230
* Get double property value from bean
231
* @param bean the target bean
232
* @param param the property name
233
* @return double value of property
234
*/
235
public double getDouble(Object bean, String param);
236
237
/**
238
* Get double property value from bean with default value
239
* @param bean the target bean
240
* @param param the property name
241
* @param defaultValue the default value if property not found
242
* @return double value of property or default value
243
*/
244
public double getDouble(Object bean, String param, double defaultValue);
245
246
/**
247
* Get double property value from bean without throwing exceptions
248
* @param bean the target bean
249
* @param param the property name
250
* @return double value or 0.0 if property not found
251
*/
252
public double getDoubleSilent(Object bean, String param);
253
254
/**
255
* Get double property value from bean without throwing exceptions with default value
256
* @param bean the target bean
257
* @param param the property name
258
* @param defaultValue the default value if property not found
259
* @return double value or default value if property not found
260
*/
261
public double getDoubleSilent(Object bean, String param, double defaultValue);
262
263
/**
264
* Get float property value from bean
265
* @param bean the target bean
266
* @param param the property name
267
* @return float value of property
268
*/
269
public float getFloat(Object bean, String param);
270
271
/**
272
* Get float property value from bean with default value
273
* @param bean the target bean
274
* @param param the property name
275
* @param defaultValue the default value if property not found
276
* @return float value of property or default value
277
*/
278
public float getFloat(Object bean, String param, float defaultValue);
279
280
/**
281
* Get float property value from bean without throwing exceptions
282
* @param bean the target bean
283
* @param param the property name
284
* @return float value or 0.0f if property not found
285
*/
286
public float getFloatSilent(Object bean, String param);
287
288
/**
289
* Get float property value from bean without throwing exceptions with default value
290
* @param bean the target bean
291
* @param param the property name
292
* @param defaultValue the default value if property not found
293
* @return float value or default value if property not found
294
*/
295
public float getFloatSilent(Object bean, String param, float defaultValue);
296
297
/**
298
* Get short property value from bean
299
* @param bean the target bean
300
* @param param the property name
301
* @return short value of property
302
*/
303
public short getShort(Object bean, String param);
304
305
/**
306
* Get short property value from bean with default value
307
* @param bean the target bean
308
* @param param the property name
309
* @param defaultValue the default value if property not found
310
* @return short value of property or default value
311
*/
312
public short getShort(Object bean, String param, short defaultValue);
313
314
/**
315
* Get short property value from bean without throwing exceptions
316
* @param bean the target bean
317
* @param param the property name
318
* @return short value or 0 if property not found
319
*/
320
public short getShortSilent(Object bean, String param);
321
322
/**
323
* Get short property value from bean without throwing exceptions with default value
324
* @param bean the target bean
325
* @param param the property name
326
* @param defaultValue the default value if property not found
327
* @return short value or default value if property not found
328
*/
329
public short getShortSilent(Object bean, String param, short defaultValue);
330
331
/**
332
* Get Object property value from bean
333
* @param bean the target bean
334
* @param param the property name
335
* @return Object value of property
336
*/
337
public Object getObject(Object bean, String param);
338
339
/**
340
* Get Object property value from bean with default value
341
* @param bean the target bean
342
* @param param the property name
343
* @param defaultValue the default value if property not found
344
* @return Object value of property or default value
345
*/
346
public Object getObject(Object bean, String param, Object defaultValue);
347
348
/**
349
* Get Object property value from bean without throwing exceptions
350
* @param bean the target bean
351
* @param param the property name
352
* @return Object value or null if property not found
353
*/
354
public Object getObjectSilent(Object bean, String param);
355
356
/**
357
* Get Object property value from bean without throwing exceptions with default value
358
* @param bean the target bean
359
* @param param the property name
360
* @param defaultValue the default value if property not found
361
* @return Object value or default value if property not found
362
*/
363
public Object getObjectSilent(Object bean, String param, Object defaultValue);
364
365
/**
366
* Get property type from bean
367
* @param bean the target bean
368
* @param param the property name
369
* @return Class type of the property
370
*/
371
public Class<?> getObjectType(Object bean, String param);
372
373
/**
374
* Get property type from bean with default value
375
* @param bean the target bean
376
* @param param the property name
377
* @param defaultValue the default class type if property not found
378
* @return Class type of the property or default value
379
*/
380
public Class<?> getObjectType(Object bean, String param, Class<?> defaultValue);
381
382
/**
383
* Get property type from bean without throwing exceptions
384
* @param bean the target bean
385
* @param param the property name
386
* @return Class type of the property or null if property not found
387
*/
388
public Class<?> getObjectTypeSilent(Object bean, String param);
389
390
/**
391
* Get property type from bean without throwing exceptions with default value
392
* @param bean the target bean
393
* @param param the property name
394
* @param defaultValue the default class type if property not found
395
* @return Class type of the property or default value if property not found
396
*/
397
public Class<?> getObjectTypeSilent(Object bean, String param, Class<?> defaultValue);
398
399
/**
400
* Get string property value from bean
401
* @param bean the target bean
402
* @param param the property name
403
* @return string value of property
404
*/
405
public String getString(Object bean, String param);
406
407
/**
408
* Get string property value from bean with default value
409
* @param bean the target bean
410
* @param param the property name
411
* @param defaultValue the default value if property not found
412
* @return string value of property or default value
413
*/
414
public String getString(Object bean, String param, String defaultValue);
415
416
/**
417
* Get string property value from bean without throwing exceptions
418
* @param bean the target bean
419
* @param param the property name
420
* @return string value or null if property not found
421
*/
422
public String getStringSilent(Object bean, String param);
423
424
/**
425
* Get string property value from bean without throwing exceptions with default value
426
* @param bean the target bean
427
* @param param the property name
428
* @param defaultValue the default value if property not found
429
* @return string value or default value if property not found
430
*/
431
public String getStringSilent(Object bean, String param, String defaultValue);
432
433
/**
434
* Copy properties from source bean to target bean
435
* @param source the source bean
436
* @param target the target bean
437
*/
438
public void copyProperties(Object source, Object target);
439
440
/**
441
* Create deep copy of bean using serialization
442
* @param source the bean to copy
443
* @return deep copy of the source bean
444
* @throws Exception if serialization fails
445
*/
446
public <T> T deepCopyProperties(Object source) throws Exception;
447
448
/**
449
* Set bean properties from HTTP request parameters
450
* @param bean the target bean
451
* @param request the HTTP servlet request
452
*/
453
public void setProperties(Object bean, HttpServletRequest request);
454
455
/**
456
* Set bean properties from HTTP request parameters with ignored properties
457
* @param bean the target bean
458
* @param request the HTTP servlet request
459
* @param ignoreProperties array of property names to ignore
460
*/
461
public void setProperties(Object bean, HttpServletRequest request, String[] ignoreProperties);
462
463
/**
464
* Set a single property on the bean
465
* @param bean the target bean
466
* @param param the property name
467
* @param value the value to set
468
*/
469
public void setProperty(Object bean, String param, Object value);
470
471
/**
472
* Set a single property on the bean without throwing exceptions
473
* @param bean the target bean
474
* @param param the property name
475
* @param value the value to set
476
*/
477
public void setPropertySilent(Object bean, String param, Object value);
478
}
479
```
480
481
**Usage Examples:**
482
483
```java
484
BeanPropertiesImpl beanProps = new BeanPropertiesImpl();
485
486
// Get properties of various types
487
boolean active = beanProps.getBoolean(user, "active");
488
int age = beanProps.getInteger(user, "age");
489
String name = beanProps.getString(user, "name");
490
491
// Silent property access (no exceptions)
492
boolean isActive = beanProps.getBooleanSilent(user, "active");
493
String email = beanProps.getStringSilent(user, "email");
494
495
// Copy properties between beans
496
User sourceUser = getUserFromDatabase();
497
User targetUser = new User();
498
beanProps.copyProperties(sourceUser, targetUser);
499
500
// Deep copy using serialization
501
User originalUser = getUserFromDatabase();
502
User clonedUser = (User) beanProps.deepCopyProperties(originalUser);
503
504
// Set properties from HTTP request
505
HttpServletRequest request = getRequest();
506
User user = new User();
507
beanProps.setProperties(user, request);
508
```
509
510
### Velocity Bean Handler
511
512
Proxy handler enabling Velocity templates to access beans with proper ClassLoader context management.
513
514
```java { .api }
515
/**
516
* InvocationHandler implementation for Velocity template engine bean access
517
*/
518
public class VelocityBeanHandler implements InvocationHandler {
519
520
/**
521
* Constructor creating handler for specified bean
522
* @param bean the target bean to proxy
523
*/
524
public VelocityBeanHandler(Object bean);
525
526
/**
527
* Proxies method calls with proper ClassLoader context
528
* @param proxy the proxy instance
529
* @param method the method being invoked
530
* @param arguments the method arguments
531
* @return the method result
532
* @throws Throwable if method invocation fails
533
*/
534
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable;
535
}
536
```
537
538
**Usage Examples:**
539
540
```java
541
// Create proxy for Velocity template access
542
Object originalBean = getMyService();
543
VelocityBeanHandler handler = new VelocityBeanHandler(originalBean);
544
545
// Create proxy instance
546
Object proxy = Proxy.newProxyInstance(
547
originalBean.getClass().getClassLoader(),
548
originalBean.getClass().getInterfaces(),
549
handler
550
);
551
552
// Use proxy in Velocity context
553
VelocityContext context = new VelocityContext();
554
context.put("myService", proxy);
555
```
556
557
## Integration with Portal Framework
558
559
The bean management system integrates deeply with the Liferay portal framework:
560
561
- **ApplicationContext Integration** - Works with Spring ApplicationContext for bean lifecycle management
562
- **Velocity Template Support** - Special handling for Velocity template engine integration
563
- **Portal Service Integration** - Enables dependency injection for portal services
564
- **HTTP Request Binding** - Direct binding of HTTP parameters to bean properties
565
- **ClassLoader Management** - Proper ClassLoader handling for OSGi environments
566
567
## Error Handling
568
569
Common exceptions and error scenarios:
570
571
- **BeanLocatorException** - Thrown when beans cannot be located by name or type
572
- **PropertyAccessException** - Thrown when bean properties cannot be accessed or set
573
- **SerializationException** - Thrown during deep copy operations if beans are not serializable
574
- **ClassCastException** - Thrown when type casting fails during bean location
575
576
Best practices include using silent methods for optional properties and implementing proper exception handling for bean location operations.