spring-cloud / spring-cloud/spring-cloud-config

@RefreshScope and component-scanned @ControllerAdvice beans.

Open
#1,452 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation help wanted
Dominant language
Java
Stars
2k
Forks
1.3k
Avg merge
2d 59m
Merged PRs (30d)
16

Description

This is in 2.1.3.RELEASE, and Spring Boot 2.1.7.

Beans that are annotated with both @RefreshScope and @ControllerAdvice, and which are component-scanned (as opposed to explicit configuration) have their @ModelAttribute methods executed twice per incoming request, as opposed to once. One of the executions is via the proxy, the other is direct.

Simple example:

package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestMain
{
    public static void main(final String[] args)
    {
        SpringApplication.run(TestMain.class, args);
    }
}

package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;

@RefreshScope
@ControllerAdvice
public class TestAdvice
{
    private static final Logger logger = LoggerFactory.getLogger(TestAdvice.class);

    @Value("${test}")
    private String testConfigValue;
    
    @ModelAttribute
    public void adviceMethod(final HttpServletRequest httpServletRequest)
    {
        logger.info("advice method executed with " + testConfigValue);
    }
}

package test;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController
{
    @GetMapping(value = "/test")
    public String test()
    {
        return "test";
    }
}

Requests to the controller log as so:

2019-08-20 14:05:07.237 INFO 5156 --- [nio-8080-exec-5] test.TestAdvice : advice method executed with testvalue
2019-08-20 14:05:07.237 INFO 5156 --- [nio-8080-exec-5] test.TestAdvice : advice method executed with testvalue

Remove the @RefreshScope, and they log just once.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Java reproduction in the issue and compare request logs with both @RefreshScope and @ControllerAdvice present versus @RefreshScope removed. Trace component scanning, proxy creation, and request handling to determine why the @ModelAttribute method runs twice; done means it runs exactly once for the scoped, component-scanned advice bean.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.