Fixing BeanNotOfRequiredTypeException on Spring proxy cast on a non-singleton bean?

后端 未结 9 1997
走了就别回头了
走了就别回头了 2020-12-05 06:59

I\'m having an issue with pulling a Spring bean from an application context.

When I try;

InnerThread instance = (InnerThread) SpringContextFactory.g         


        
相关标签:
9条回答
  • 2020-12-05 07:34

    Once again, after spending hours trying to debug this I find the answer right after posting on StackOverflow.

    A key point that I left out from my question is that InnerThread has a transactional method (sorry thought this was irrelevant). This is the important difference between OuterThread and InnerThread.

    From the Spring documentation:

    Note

    Multiple sections are collapsed into a single unified auto-proxy creator at runtime, which applies the strongest proxy settings that any of the sections (typically from different XML bean definition files) specified. This also applies to the and elements.

    To be clear: using 'proxy-target-class="true"' on , or elements will force the use of CGLIB proxies for all three of them.

    Adding the above to my configuration (based in persistance-context.xml, which you can see loaded above) line seems to fix the problem. However, I think this may be a quick fix workaround as opposed to real solution.

    I think I've got a few deeper issues here, number one being that I find Spring as confusing as expletive deleted. Second I should probably be using Spring's TaskExecutor to kick off my threads. Third my threads should implement Runnable instead of extending Thread (See SO question below).

    See Also

    • BeanNotOfRequiredTypeException using ServiceLocatorFactoryBean and @Transactional (Nothing better then finding a thread off Google after hours of searching to have a response that says "This has already been answered a million times.")
    • Section 6.6 in the Spring Docs.
    • Java: “implements Runnable” vs. “extends Thread”
    0 讨论(0)
  • 2020-12-05 07:35

    This is just a guess, but try making an interface InnerThreadInterface, then let InnerThread class extend it.

    After that you should be able to do:

    InnerThreadInterface inner = ctx.getBean("innerThread", InnerThread.class);

    0 讨论(0)
  • 2020-12-05 07:38

    I had this issue even though I referenced CGLIB and used the proxy-target-class="true" setting. I determined ehcache:annotation tag was to blame... Removing the following configuration solved this for me. Fortunately, I was able to use hibernate level 2 caching instead of having to use ehcache declarative caching.

    <ehcache:annotations>
        <ehcache:caching id="myCacheModel" cacheName="myCache"/>
        <ehcache:flushing id="myFlushModel" cacheNames="myCache" when="after"/>
    </ehcache:annotations> 
    
    0 讨论(0)
  • 2020-12-05 07:42

    create a DAO interface and implement the DAO interface to the SERVICE class and provide the config details of SERVICE class in the applicationContext.xml file i.e., the spring config file and access the bean using the bean id and reference the instance of the so instantiated proxy object to the DAO interface ... it will work perfectly fine ....

    0 讨论(0)
  • 2020-12-05 07:48

    One way to solve this issue is by extending the runnable interface and creating your own:

    public interface MyInterface extends Runnable {
      // your own method declarations here
      void doSomething();
      ...
    }
    

    Then you should implement your interface instead of the runnable:

    @Component("myInterface")
    @Scope("prototype")
         public class MyInterfaceImpl implements MyInterface {
    
              // your own method declarations here
              public void doSomething(){
              ...
              }
    
    
              // implement run from Runnable Interface
              @Transactional
              public void run(){
                    .....
              }
    
                 ...
            }
    

    This will work fine:

    ...
        MyInterface mynterface = SpringApplicationContext.getBean("myInterface", MyInterface.class);
    
        myInterface.doSomething();
      ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(myInterface);
    ...
    
    0 讨论(0)
  • 2020-12-05 07:49

    I had the same problem:

    This is just a guess, but try making an interface InnerThreadInterface, then let InnerThread class extend it. After that you should be able to do:

    I use this way instead of what is posted above and it worked fine. There was no need of setting the proxy-target-class to true, this required more libraries that wasn't on my classpath.

    InnerThreadInterface inner = (InnerThreadInterface)ctx.getBean("innerThread");
    
    0 讨论(0)
提交回复
热议问题