0

I have a SpringBoot application that uses a library (which I cannot modify) that is injecting a bean of type "String" in the context like this:

@Bean
public String someBean() {
         return "Some String";
}

This is not causing any problem until I declare an aspect like this:

@Aspect
@Component
public class UseCaseAspect {

    @Around("@target(some.specific.annotation.MyAnnotation)")
    public Object doOperation(ProceedingJoinPoint pjp) {
        //Do nothing yet.
    }
}

Important: the aspect does NOT affect the bean (because it affects annotated beans only, and that is a custom annotation that the library containing the bean doesn't even know).

When Spring detects the Aspect, something changes in the way Spring loads the context because I get an exception like:

Caused by: java.lang.IllegalArgumentException: Cannot subclass final class java.lang.String
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:657)
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)

Beacuse it apparently tries to proxy the bean "someBean" and it is not able because it GCLIB cannot subclass the bean (because it is a String).

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someBean' defined in class path resource [the/path/to/the/bean/MyClass.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class java.lang.String: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class java.lang.String
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)

Any hint? Thank you very much!

Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77
  • 1
    This [answer](https://stackoverflow.com/a/53452483/4214241) from kriegaex explains the behaviour . Please go through. – R.G Aug 07 '20 at 08:53
  • Thank you ver much! :) – Mr.Eddart Aug 07 '20 at 08:59
  • Np . If you are interested about the pointcut expression in the answer to that question , please go through kriegaex's [answer](https://stackoverflow.com/q/61430240/4214241) to my question as well – R.G Aug 07 '20 at 09:02

0 Answers0