spring-projects/spring-framework · error · InvalidPropertyException

Invalid property '{field.getName()}' of bean class [{beanCla

Error message

Invalid property '{field.getName()}' of bean class [{beanClass.getName()}]: Field is not accessible

What it means

Thrown as InvalidPropertyException by DirectFieldAccessor.FieldPropertyHandler.getValue when field.get(wrappedInstance) raises IllegalAccessException or java.lang.reflect.InaccessibleObjectException, even after ReflectionUtils.makeAccessible. Indicates the JVM refuses reflective read access to the field.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/DirectFieldAccessor.java:147

		@Override
		public TypeDescriptor getCollectionType(int nestingLevel) {
			return new TypeDescriptor(this.resolvableType.getNested(nestingLevel).asCollection().getGeneric(),
					null, this.field.getAnnotations());
		}

		@Override
		public @Nullable TypeDescriptor nested(int level) {
			return TypeDescriptor.nested(this.field, level);
		}

		@Override
		public @Nullable Object getValue() throws Exception {
			try {
				ReflectionUtils.makeAccessible(this.field);
				return this.field.get(getWrappedInstance());
			}
			catch (IllegalAccessException | InaccessibleObjectException ex) {
				throw new InvalidPropertyException(getWrappedClass(),
						this.field.getName(), "Field is not accessible", ex);
			}
		}

		@Override
		public void setValue(@Nullable Object value) throws Exception {
			try {
				ReflectionUtils.makeAccessible(this.field);
				this.field.set(getWrappedInstance(), value);
			}
			catch (IllegalAccessException | InaccessibleObjectException ex) {
				throw new InvalidPropertyException(getWrappedClass(), this.field.getName(),
						"Field is not accessible", ex);
			}
		}
	}

}

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Add --add-opens=<module>/<package>=ALL-UNNAMED (or =spring.beans) to the JVM args for the offending module/package.
  2. Ensure the field is on a class you control and is at least package/private accessible from a location Spring can reach; prefer public fields for cross-module access.
  3. If you cannot open the module, switch to a BeanWrapper that uses public getters/setters instead of field access.

Example fix

// before: java --add-opens absent, DirectFieldAccessor reads a JDK-class field
DirectFieldAccessor accessor = new DirectFieldAccessor(jdkInternal);
accessor.getPropertyValue("field");

// after: launch with
// java --add-opens=java.base/java.lang=ALL-UNNAMED ...
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight: can we reflectively read the field?
try {
    ReflectionUtils.makeAccessible(field);
    field.get(bean);
} catch (IllegalAccessException | InaccessibleObjectException ex) {
    // need --add-opens or different access strategy
}

Type guard

static boolean fieldReadable(Field f, Object owner) {
    try { ReflectionUtils.makeAccessible(f); f.get(owner); return true; }
    catch (IllegalAccessException | InaccessibleObjectException e) { return false; }
}

Try / catch

try {
    accessor.getPropertyValue(name);
} catch (InvalidPropertyException ex) {
    if (ex.getMessage().contains("not accessible")) {
        // add --add-opens to JVM, or switch to BeanWrapper with public getters
    }
}

Prevention

When it happens

Trigger: DirectFieldAccessor reads a field via reflection; if the field belongs to a class in another module that has not been opened to Spring, makeAccessible/setAccessible throws InaccessibleObjectException, which is caught and rethrown as InvalidPropertyException 'Field is not accessible'.

Common situations: Running on JDK 16+ where strong encapsulation is enforced by default, accessing a field of a JDK class or a third-party module that has not opened its package to the application, or a SecurityManager denying access.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/8ac207edbfc16b44. Report an issue: GitHub.