24
24
import java .security .AccessControlContext ;
25
25
import java .security .AccessController ;
26
26
import java .security .PrivilegedAction ;
27
+ import java .security .PrivilegedActionException ;
28
+ import java .security .PrivilegedExceptionAction ;
27
29
import java .util .ArrayList ;
28
30
import java .util .Arrays ;
29
31
import java .util .Collection ;
@@ -330,13 +332,27 @@ public Object createBean(Class beanClass, int autowireMode, boolean dependencyCh
330
332
331
333
public Object autowire (Class beanClass , int autowireMode , boolean dependencyCheck ) throws BeansException {
332
334
// Use non-singleton bean definition, to avoid registering bean as dependent bean.
333
- RootBeanDefinition bd = new RootBeanDefinition (beanClass , autowireMode , dependencyCheck );
335
+ final RootBeanDefinition bd = new RootBeanDefinition (beanClass , autowireMode , dependencyCheck );
334
336
bd .setScope (BeanDefinition .SCOPE_PROTOTYPE );
335
337
if (bd .getResolvedAutowireMode () == AUTOWIRE_CONSTRUCTOR ) {
336
338
return autowireConstructor (beanClass .getName (), bd , null , null ).getWrappedInstance ();
337
339
}
338
340
else {
339
- Object bean = getInstantiationStrategy ().instantiate (bd , null , this );
341
+ Object bean = null ;
342
+ final BeanFactory parent = this ;
343
+
344
+ if (System .getSecurityManager () != null ) {
345
+ bean = AccessController .doPrivileged (new PrivilegedAction <Object >() {
346
+
347
+ public Object run () {
348
+ return getInstantiationStrategy ().instantiate (bd , null , parent );
349
+ }
350
+ }, getAccessControlContext ());
351
+ }
352
+ else {
353
+ bean = getInstantiationStrategy ().instantiate (bd , null , parent );
354
+ }
355
+
340
356
populateBean (beanClass .getName (), bd , new BeanWrapperImpl (bean ));
341
357
return bean ;
342
358
}
@@ -403,9 +419,6 @@ public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, St
403
419
protected Object createBean (final String beanName , final RootBeanDefinition mbd , final Object [] args )
404
420
throws BeanCreationException {
405
421
406
- AccessControlContext acc = AccessController .getContext ();
407
- return AccessController .doPrivileged (new PrivilegedAction <Object >() {
408
- public Object run () {
409
422
if (logger .isDebugEnabled ()) {
410
423
logger .debug ("Creating instance of bean '" + beanName + "'" );
411
424
}
@@ -438,8 +451,6 @@ public Object run() {
438
451
logger .debug ("Finished creating instance of bean '" + beanName + "'" );
439
452
}
440
453
return beanInstance ;
441
- }
442
- }, acc );
443
454
}
444
455
445
456
/**
@@ -904,9 +915,22 @@ protected Constructor[] determineConstructorsFromBeanPostProcessors(Class beanCl
904
915
* @param mbd the bean definition for the bean
905
916
* @return BeanWrapper for the new instance
906
917
*/
907
- protected BeanWrapper instantiateBean (String beanName , RootBeanDefinition mbd ) {
918
+ protected BeanWrapper instantiateBean (final String beanName , final RootBeanDefinition mbd ) {
908
919
try {
909
- Object beanInstance = getInstantiationStrategy ().instantiate (mbd , beanName , this );
920
+ Object beanInstance = null ;
921
+ final BeanFactory parent = this ;
922
+ if (System .getSecurityManager () != null ) {
923
+ beanInstance = AccessController .doPrivileged (new PrivilegedAction <Object >() {
924
+
925
+ public Object run () {
926
+ return getInstantiationStrategy ().instantiate (mbd , beanName , parent );
927
+ }
928
+ }, getAccessControlContext ());
929
+ }
930
+ else {
931
+ beanInstance = getInstantiationStrategy ().instantiate (mbd , beanName , parent );
932
+ }
933
+
910
934
BeanWrapper bw = new BeanWrapperImpl (beanInstance );
911
935
initBeanWrapper (bw );
912
936
return bw ;
@@ -1229,6 +1253,12 @@ protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrap
1229
1253
1230
1254
MutablePropertyValues mpvs = null ;
1231
1255
List <PropertyValue > original ;
1256
+
1257
+ if (System .getSecurityManager ()!= null ) {
1258
+ if (bw instanceof BeanWrapperImpl ) {
1259
+ ((BeanWrapperImpl ) bw ).setSecurityContext (getAccessControlContext ());
1260
+ }
1261
+ }
1232
1262
1233
1263
if (pvs instanceof MutablePropertyValues ) {
1234
1264
mpvs = (MutablePropertyValues ) pvs ;
@@ -1337,19 +1367,20 @@ private Object convertForProperty(Object value, String propertyName, BeanWrapper
1337
1367
* @see #invokeInitMethods
1338
1368
* @see #applyBeanPostProcessorsAfterInitialization
1339
1369
*/
1340
- protected Object initializeBean (String beanName , Object bean , RootBeanDefinition mbd ) {
1341
- if (bean instanceof BeanNameAware ) {
1342
- ((BeanNameAware ) bean ).setBeanName (beanName );
1343
- }
1344
-
1345
- if (bean instanceof BeanClassLoaderAware ) {
1346
- ((BeanClassLoaderAware ) bean ).setBeanClassLoader (getBeanClassLoader ());
1370
+ protected Object initializeBean (final String beanName , final Object bean , RootBeanDefinition mbd ) {
1371
+
1372
+ if (System .getSecurityManager () != null ) {
1373
+ AccessController .doPrivileged (new PrivilegedAction <Object >() {
1374
+ public Object run () {
1375
+ invokeAwareMethods (beanName , bean );
1376
+ return null ;
1377
+ }
1378
+ }, getAccessControlContext ());
1347
1379
}
1348
-
1349
- if (bean instanceof BeanFactoryAware ) {
1350
- ((BeanFactoryAware ) bean ).setBeanFactory (this );
1380
+ else {
1381
+ invokeAwareMethods (beanName , bean );
1351
1382
}
1352
-
1383
+
1353
1384
Object wrappedBean = bean ;
1354
1385
if (mbd == null || !mbd .isSynthetic ()) {
1355
1386
wrappedBean = applyBeanPostProcessorsBeforeInitialization (wrappedBean , beanName );
@@ -1369,6 +1400,20 @@ protected Object initializeBean(String beanName, Object bean, RootBeanDefinition
1369
1400
}
1370
1401
return wrappedBean ;
1371
1402
}
1403
+
1404
+ private void invokeAwareMethods (final String beanName , final Object bean ) {
1405
+ if (bean instanceof BeanNameAware ) {
1406
+ ((BeanNameAware ) bean ).setBeanName (beanName );
1407
+ }
1408
+
1409
+ if (bean instanceof BeanClassLoaderAware ) {
1410
+ ((BeanClassLoaderAware ) bean ).setBeanClassLoader (getBeanClassLoader ());
1411
+ }
1412
+
1413
+ if (bean instanceof BeanFactoryAware ) {
1414
+ ((BeanFactoryAware ) bean ).setBeanFactory (AbstractAutowireCapableBeanFactory .this );
1415
+ }
1416
+ }
1372
1417
1373
1418
/**
1374
1419
* Give a bean a chance to react now all its properties are set,
@@ -1382,15 +1427,30 @@ protected Object initializeBean(String beanName, Object bean, RootBeanDefinition
1382
1427
* @throws Throwable if thrown by init methods or by the invocation process
1383
1428
* @see #invokeCustomInitMethod
1384
1429
*/
1385
- protected void invokeInitMethods (String beanName , Object bean , RootBeanDefinition mbd )
1430
+ protected void invokeInitMethods (String beanName , final Object bean , RootBeanDefinition mbd )
1386
1431
throws Throwable {
1387
1432
1388
1433
boolean isInitializingBean = (bean instanceof InitializingBean );
1389
1434
if (isInitializingBean && (mbd == null || !mbd .isExternallyManagedInitMethod ("afterPropertiesSet" ))) {
1390
1435
if (logger .isDebugEnabled ()) {
1391
1436
logger .debug ("Invoking afterPropertiesSet() on bean with name '" + beanName + "'" );
1392
1437
}
1393
- ((InitializingBean ) bean ).afterPropertiesSet ();
1438
+
1439
+ if (System .getSecurityManager () != null ) {
1440
+ try {
1441
+ AccessController .doPrivileged (new PrivilegedExceptionAction <Object >() {
1442
+ public Object run () throws Exception {
1443
+ ((InitializingBean ) bean ).afterPropertiesSet ();
1444
+ return null ;
1445
+ }
1446
+ },getAccessControlContext ());
1447
+ } catch (PrivilegedActionException pae ) {
1448
+ throw pae .getException ();
1449
+ }
1450
+ }
1451
+ else {
1452
+ ((InitializingBean ) bean ).afterPropertiesSet ();
1453
+ }
1394
1454
}
1395
1455
1396
1456
if (mbd != null ) {
@@ -1413,9 +1473,9 @@ protected void invokeInitMethods(String beanName, Object bean, RootBeanDefinitio
1413
1473
* @param enforceInitMethod indicates whether the defined init method needs to exist
1414
1474
* @see #invokeInitMethods
1415
1475
*/
1416
- protected void invokeCustomInitMethod (String beanName , Object bean , RootBeanDefinition mbd ) throws Throwable {
1476
+ protected void invokeCustomInitMethod (String beanName , final Object bean , RootBeanDefinition mbd ) throws Throwable {
1417
1477
String initMethodName = mbd .getInitMethodName ();
1418
- Method initMethod = (mbd .isNonPublicAccessAllowed () ?
1478
+ final Method initMethod = (mbd .isNonPublicAccessAllowed () ?
1419
1479
BeanUtils .findMethod (bean .getClass (), initMethodName ) :
1420
1480
ClassUtils .getMethodIfAvailable (bean .getClass (), initMethodName ));
1421
1481
if (initMethod == null ) {
@@ -1437,11 +1497,23 @@ protected void invokeCustomInitMethod(String beanName, Object bean, RootBeanDefi
1437
1497
logger .debug ("Invoking init method '" + initMethodName + "' on bean with name '" + beanName + "'" );
1438
1498
}
1439
1499
ReflectionUtils .makeAccessible (initMethod );
1440
- try {
1441
- initMethod .invoke (bean , (Object []) null );
1500
+ if (System .getSecurityManager () != null ) {
1501
+ try {
1502
+ AccessController .doPrivileged (new PrivilegedExceptionAction <Object >() {
1503
+
1504
+ public Object run () throws Exception {
1505
+ initMethod .invoke (bean , (Object []) null );
1506
+ return null ;
1507
+ }
1508
+ }, getAccessControlContext ());
1509
+ }
1510
+ catch (PrivilegedActionException pae ) {
1511
+ InvocationTargetException ex = (InvocationTargetException ) pae .getException ();
1512
+ throw ex .getTargetException ();
1513
+ }
1442
1514
}
1443
- catch ( InvocationTargetException ex ) {
1444
- throw ex . getTargetException ( );
1515
+ else {
1516
+ initMethod . invoke ( bean , ( Object []) null );
1445
1517
}
1446
1518
}
1447
1519
0 commit comments