简体   繁体   中英

How to inject RequestScope bean in spring boot wicket application

In my Wicket page I have:

@SpringBean
protected J2EContext j2EContext;

In Configuration I have:

@Bean
@RequestScope
public J2EContext getWebContext() {
    return new J2EContext(request, response, getSessionStore());
}


@Bean
public RequestContextListener requestContextListener() {
    return new RequestContextListener();
}

application starts OK but when I go to home page I got exception:

Caused by: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
    at net.sf.cglib.proxy.Enhancer.emitConstructors(Enhancer.java:931)
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:631)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:

329)
    at net.sf.cglib.proxy.Enhancer.generate(Enhancer.java:492)
    at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93)
    at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91)
    at net.sf.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)

when wicket want to inject J2EContext bean. I think this is because page is rendering before @RequestScope create this bean. Any idea how to fix this problem ?

to inject beans Wicket creates a proxy in two different ways:

  • If bean is an interface a standard java.lang.reflect.Proxy is created
  • If bean is a class cglib is used by default

Unfortunately cglib can not create proxy for classes without a default constructor. There is however a third way that can solve the problem. Just add objenesis as dependency to your project and Wicket will use it instead of cglib.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM