diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java index 79aa3441d01..241bd29b976 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/SimpleInstantiationStrategy.java @@ -54,12 +54,15 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { - constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction() { - - public Constructor run() throws Exception { - return clazz.getDeclaredConstructor((Class[]) null); - } - }); + if (System.getSecurityManager() != null) { + constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction() { + public Constructor run() throws Exception { + return clazz.getDeclaredConstructor((Class[]) null); + } + }); + } else { + constructorToUse = clazz.getDeclaredConstructor((Class[]) null); + } beanDefinition.resolvedConstructorOrFactoryMethod = constructorToUse; } catch (Exception ex) { @@ -127,14 +130,19 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy { Object factoryBean, final Method factoryMethod, Object[] args) { try { + if (System.getSecurityManager() != null) { + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + ReflectionUtils.makeAccessible(factoryMethod); + return null; + } + }); + } + else { + ReflectionUtils.makeAccessible(factoryMethod); + } + // It's a static method if the target is null. - AccessController.doPrivileged(new PrivilegedAction() { - - public Object run() { - ReflectionUtils.makeAccessible(factoryMethod); - return null; - } - }); return factoryMethod.invoke(factoryBean, args); } catch (IllegalArgumentException ex) { @@ -151,5 +159,4 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy { "Factory method [" + factoryMethod + "] threw exception", ex.getTargetException()); } } - -} +} \ No newline at end of file