This question already has answers here:
Java config for spring interceptor where interceptor is using autowired spring beans
                                
                                    (3个答案)
                                
                        
                                3年前关闭。
            
                    
@Component
public class AuthorizeInterceptor extends HandlerInterceptorAdapter {

    private Logger log = LoggerFactory.getLogger(AuthorizeInterceptor.class);

    @Autowired
    private Environment env;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("env:",env);
        return true;
    }
}


env是null,某些使用@Value()的配置也无法获取值,总是null
我们添加这个拦截器registry.addInterceptor(new Interceptor()).addPathPatterns("/**");new
获得价值的一种方法是BeanFactory.getBean("beanname")
但是我怎样才能让Spring Manager这个拦截器,所以我可以使用@Autowired

最佳答案

应该是log.info("env: {}", env);

我相信您忘记了在记录器调用中放置占位符{}

09-11 20:20