spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.42k stars 38.07k forks source link

Reflection problem instantiating bean [SPR-233] #4964

Closed spring-projects-issues closed 20 years ago

spring-projects-issues commented 20 years ago

Michael Brown opened SPR-233 and commented

I'm trying to wire up an application using netty 1.5.0(http://gleamynode.net/dev/projects/netty2) and getting an exception when I put the following bean in my application context xml file:

<bean id="eventDispatcher" class="net.gleamynode.netty2.OrderedEventDispatcher" init-method="start">
    <property name="threadPoolSize">
        <value>1</value>
    </property>
</bean>

The exception I get is:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eventDispatcher' defined in file [V:\dev\projects\sumup\config\serverContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException: PropertyAccessExceptionsException (1 errors); nested propertyAccessExceptions are: [org.springframework.beans.MethodInvocationException: Property 'threadPoolSize' threw exception; nested exception is java.lang.IllegalAccessException: Class org.springframework.beans.BeanWrapperImpl can not access a member of class net.gleamynode.netty2.AbstractThreadPooledEventDispatcher with modifiers "public synchronized"] PropertyAccessExceptionsException (1 errors) org.springframework.beans.MethodInvocationException: Property 'threadPoolSize' threw exception; nested exception is java.lang.IllegalAccessException: Class org.springframework.beans.BeanWrapperImpl can not access a member of class net.gleamynode.netty2.AbstractThreadPooledEventDispatcher with modifiers "public synchronized" java.lang.IllegalAccessException: Class org.springframework.beans.BeanWrapperImpl can not access a member of class net.gleamynode.netty2.AbstractThreadPooledEventDispatcher with modifiers "public synchronized" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57) at java.lang.reflect.Method.invoke(Method.java:317) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:718) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:624) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:755) at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:782) at org.springframework.beans.BeanWrapperImpl.setPropertyValues(BeanWrapperImpl.java:771) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:763) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:604) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:267) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:204) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:136) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:209) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:279) at org.springframework.context.support.FileSystemXmlApplicationContext.\(FileSystemXmlApplicationContext.java:83) at org.springframework.context.support.FileSystemXmlApplicationContext.\(FileSystemXmlApplicationContext.java:68) at org.springframework.context.support.FileSystemXmlApplicationContext.\(FileSystemXmlApplicationContext.java:59) at com.baesystems.etss.etpg.listener.Server.main(Server.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324)

This was working with netty 1.4.0. The only difference that I can see is that the method setThreadPoolSize has been moved to an abstract base class.


Affects: 1.1 RC1

spring-projects-issues commented 20 years ago

Juergen Hoeller commented

Actually, that IllegalAccessException is thrown by the JDK reflection mechanism, not by Spring's BeanWrapper itself.

I bet that Netty's AbstractThreadPooledEventDispatcher class is protected, which does not allow any other package to see the class via plain reflection, therefore also not the properties of this class.

That's a bit odd, as you can invoke the setter in a conventional programmatic style without issues, but that's how standard reflection security checks work.

I've just added special checks to CachedIntrospectionResults, explicitly setting read and write methods accessible if the declaring class is not public. This solves the issue for my test case, so should work for Netty's AbstractThreadPooledEventDispatcher too.

Juergen

spring-projects-issues commented 20 years ago

Michael Brown commented

Jurgen, You are correct. I took a look at his code and the abstract base class is not public (package access).