spring-projects / spring-boot

Spring Boot helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss.
https://spring.io/projects/spring-boot
Apache License 2.0
75.38k stars 40.72k forks source link

using @cacheable will lead to proxy problem #16634

Closed wuyuan0717 closed 5 years ago

wuyuan0717 commented 5 years ago

springboot edition: 2.1.3.RELEASE

opening cache support:

image

adding cahce when login:

image

when i start my project up ,i find a problem , LoginController will not be as a request handler. maybe it had be proxied by cache support, image

isHandler(beanType) retrun false,but LoginController should be a controller

if (beanType != null && isHandler(beanType)) { detectHandlerMethods(beanName); }

16630

wuyuan0717 commented 5 years ago

How can I solve this problem?

snicoll commented 5 years ago

@wuyuan0717 screenshots are not a great way to get support and I am not sure to understand what you're trying to achieve by caching that login method.

LoginController will not be as a request handler.

Sorry but I don't understand what that means. Can you please a small sample that reproduces the problem you're experiencing (a zip or a link to a github repo).

snicoll commented 5 years ago

@wuyuan0717 I can see you've already submitted this an hour ago #16630 and you were redirected to ask a question on StackOverflow. Ignoring a reply from the team and opening a duplicate issue is not a great way to get support either.

wuyuan0717 commented 5 years ago

@snicoll when i using '@EnableCaching' open cache support ,and i add '@Cacheable' on a method that need cache support. AbstractHandlerMethodMapping can`t register it as a handler,because this beanType is 'class com.sun.proxy.$Proxy109' ,'@RestController' and '@Requesrmapping' will not find .

    protected boolean isHandler(Class<?> beanType) {
        return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
                AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
    }

image

wuyuan0717 commented 5 years ago

@snicoll The screenshot is what I want to show my LoginContoller annotations, but he is not registered as a request processing class. When I used caching, he was proxyed and the annotations ‘@Controller’、‘@RequestMapping’ could not be found

snicoll commented 5 years ago

Alright, I am going to close this now. Take the time to read what I've asked you to provide and we can reopen this issue for investigations.

wuyuan0717 commented 5 years ago

I'm sorry for the question I asked yesterday. I've found the reason for the problem. It turned out that there was a problem with the jar of shiro-spring-boot-web-starter I added. It adds a bean of DefaultAdvisorAutoProxyCreator by default. I set the shiro.annotations.enabled to false. All the problems have been solved.

Now I have a question, why does joining bean of DefaultAdvisorAutoProxyCreator lead to proxy problems?

package org.apache.shiro.spring.boot.autoconfigure;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.config.AbstractShiroAnnotationProcessorConfiguration;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @since 1.4.0
 */
@SuppressWarnings("SpringFacetCodeInspection")
@Configuration
@ConditionalOnProperty(name = "shiro.annotations.enabled", matchIfMissing = true)
public class ShiroAnnotationProcessorAutoConfiguration extends AbstractShiroAnnotationProcessorConfiguration {

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    @ConditionalOnMissingBean
    @Override
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        return super.defaultAdvisorAutoProxyCreator();
    }

    @Bean
    @ConditionalOnMissingBean
    @Override
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        return super.authorizationAttributeSourceAdvisor(securityManager);
    }
}
snicoll commented 5 years ago

Now I have a question

Please take the time to review the guidelines before contributing again. As we've already mentioned, we prefer to use GitHub issues only for bugs and enhancements.

wuyuan0717 commented 5 years ago

I have taked some time to review the guidelines. Thanks for your sincerely advise