socia1ca3t
socia1ca3t

Reputation: 11

The CGLIB enhancement can not work after I introduce the spring-boot-starter-data-jpa

The @Cacheable and @CachePut annotation works well before I introduce the spring-boot-starter-data-jpa.

It is the bean with @Cacheable and @CachePut annotation:

@Service
public class BasicAuthRestTemplate {


    @Cacheable(value = "restTemplate", key = "#email")
    public RestTemplate getRestTemplate(String email) {

        return null;
    }

    @CachePut(value = "restTemplate", key = "#email")
    public RestTemplate getRestTemplate(String email, String password) {

        return new RestTemplateBuilder().basicAuthentication(email, password).build();
    }
}

The following is the launched log info, That is to say, when the system according to the specific conditions, decides to use the CglibAutoProxyConfiguration, the CGLIB function ceases to work

the bean can not be enhanced by CGLIB:

   AopAutoConfiguration.AspectJAutoProxyingConfiguration matched:
      - @ConditionalOnClass found required class 'org.aspectj.weaver.Advice' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration.CglibAutoProxyConfiguration matched:
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)


   AopAutoConfiguration.ClassProxyingConfiguration:
      Did not match:
         - @ConditionalOnMissingClass found unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)    
      
================================================================================================      
      
the bean can be enhaced by CGLIB :


   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
      
      
   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)
         

If I exclude the aspectjweaver in the spring-boot-starter-data-jpa dependency, it will work well as before. So I can confirm that adding this dependency caused CGLIB functionality to fail.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2023/05/31 update

In the case of not using the aspect weaver dependency, initialize the bean is completed, and then call the postprocessor (postProcessAfterInitialization), and the bean that can find spring framework.cache.interceptor.CacheInterceptor can be proxied

Breakpoint screenshot

2023/06/01 update

Perhaps the reason for this error is not the dependency on aspect weaver, because I also added a JPA dependency in a previous copy of this project, and it works just fine. Maybe this error is caused by other reasons, but I can't find the answer on the Internet, so I just keep looking at the source code

When using the aspect weaver, the org.springframework.cache.config.internalCacheAdvisorcan be found is the key to get the proxied bean, the reason why the InternalCacheAdvisor can not be added to the list of eligible advisors is that the result of 'this.beanFactory.isCurrentlyInCreation(name)' is true.

the result of 'this.beanFactory.isCurrentlyInCreation(name)' is true

2023/06/02 update

I found the same issue in Spring's GitHub : Advisor silently skipped if it is currently in creation [SPR-10430]

There is a solution that uses @Lazy:

@Lazy
@Autowired
private BasicAuthRestTemplate basicAuthRestTemplate;

But I believe there is room for improvement at the framework level.

Upvotes: 1

Views: 378

Answers (0)

Related Questions