After upgrading to spring boot 1.4.1 from 1.3.5, I noticed that my session scoped bean is registered with a PersistenceAnnotationBeanPostProcessor even though it is not an Entity.
Is there a consequence of this? Although I could workaround the issue that led me to this: (the hashcode invocation was causing NPE), I am worried about any redundant post processing
at com.xyz.config.UserSession.getUserInfo(UserSession.java:68)
at com.xyz.config.UserSession.hashCode(UserSession.java:17)
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.requiresDestruction(PersistenceAnnotationBeanPostProcessor.java:380)
at org.springframework.beans.factory.support.DisposableBeanAdapter.hasApplicableProcessors(DisposableBeanAdapter.java:431)
at org.springframework.beans.factory.support.AbstractBeanFactory.requiresDestruction(AbstractBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractBeanFactory.registerDisposableBeanIfNecessary(AbstractBeanFactory.java:1645)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:586)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:44)
at org.springframework.web.context.request.SessionScope.get(SessionScope.java:93)
- locked <0x339a> (a org.springframework.session.web.http.SessionRepositoryFilter$SessionRepositoryRequestWrapper$HttpSessionWrapper)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
Comment From: philwebb
Could you please provide the full stack-trace and ideally a sample project that shows the problem?
Comment From: wilkinsona
I think you've misinterpreted the stack trace.
When the UserSession bean is created, a check is made to see if it requires destruction which involves calling all SmartDestructionAwareBeanPostProcessors of which PersistenceAnnotationBeanPostProcessor is one. The call to UserSession.hashCode is performed to check if the UserSession instance is registered with the post-processor. It is not an indication that it is registered with that post-processor.
requiresDestruction was introduced in https://github.com/spring-projects/spring-framework/commit/fca5365cf1b8068e750b0130dabb4c0d7c667767 to fix SPR-13744 in Spring Framework 4.3. Spring Boot 1.3.x uses Spring Framework 4.2.x whereas Spring Boot 1.4.x uses Spring Framework 4.3.x, hence the change in behaviour that you saw when upgrading from 1.3 to 1.4.
In short, I don't think you need to worry and you should simply correct your hashCode implementation to avoid a NullPointerException. If the new behaviour is problematic, please open an SPR Jira ticket.
Comment From: rlarun94
Hi @wilkinsona I facing same issue as above.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'moduleDirector': Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: unexpected call to public native int java.lang.Object.hashCode()
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
I am using spring-orm - 4.3.30.RELEASE (for 4.2.9 it is working nicely), Could you please help in resolving this issue.
Comment From: rlarun94
Below is the complete trace.
2024-02-08 17:02:19,562 WARN [org.springframework.context.support.ClassPathXmlApplicationContext] [main] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'moduleDirector': Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: unexpected call to public native int java.lang.Object.hashCode() -
2024-02-08 17:02:19,573 ERROR [com.test.services.rendering.RenderingServiceDaemon] [main] CRITICAL: unable to start rendering service. Exiting -
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'moduleDirector': Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: unexpected call to public native int java.lang.Object.hashCode()
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1611)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:237)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:703)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:527)
at com.test.services.rendering.RenderingServiceDaemon.start(RenderingServiceDaemon.java:56)
at com.test.services.rendering.RenderingServiceDaemon.main(RenderingServiceDaemon.java:80)
Caused by: java.lang.UnsupportedOperationException: unexpected call to public native int java.lang.Object.hashCode()
at com.test.testutil.UnsupportedOperationFactoryBean$PlaceHolder.invoke(UnsupportedOperationFactoryBean.java:78)
at com.sun.proxy.$Proxy32.hashCode(Unknown Source)
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.requiresDestruction(PersistenceAnnotationBeanPostProcessor.java:380)
at org.springframework.beans.factory.support.DisposableBeanAdapter.hasApplicableProcessors(DisposableBeanAdapter.java:431)
at org.springframework.beans.factory.support.AbstractBeanFactory.requiresDestruction(AbstractBeanFactory.java:1674)
at org.springframework.beans.factory.support.AbstractBeanFactory.registerDisposableBeanIfNecessary(AbstractBeanFactory.java:1691)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at com.test.spring.module.ModuleInstance.loadConfig(ModuleInstance.java:65)
at com.test.spring.module.ModuleInstance.load(ModuleInstance.java:50)
at com.test.spring.module.ModuleDirector.init(ModuleDirector.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
... 13 more
Comment From: wilkinsona
You need to fix com.saepio.testutil.UnsupportedOperationFactoryBean$PlaceHolder.invoke so that calls to hashCode are allowed.
Please note that the code you're asking about is part of Spring Framework and has nothing to do with Spring Boot. Furthermore, Spring Framework 4.3.x reached the end of its life at the end of 2020 and should no longer be used.
Comment From: rlarun94
private static final class PlaceHolder implements InvocationHandler
{
private Object delegate;
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
if( delegate != null )
{
String methodName = method.getName();
if (methodName.equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (methodName.equals("hashCode")) {
// Use hashCode of proxy.
return System.identityHashCode(proxy);
}
try {
return method.invoke(this.delegate, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
throw new UnsupportedOperationException("unexpected call to "+method);
}
}
Above is my method, I am trying to create proxy beans. Could you please check what I am doing wrong
Comment From: rlarun94
We are having some tight coupling in the implementation level, we started to upgrade spring. We upgraded from 3.2.x , In Spring 4.3.30 version we are facing this issue in the current code.
Comment From: wilkinsona
As I said above, the code that you're asking about has nothing to do with Spring Boot and is no longer supported. I'm afraid that means that we cannot afford to spend time here trying to help you. Someone on Stack Overflow may be able to help you.
Comment From: rlarun94
ok thank you !!