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!