Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@RefreshScope and component-scanned @ControllerAdvice beans. #1452

Open
smar21 opened this issue Aug 20, 2019 · 4 comments
Open

@RefreshScope and component-scanned @ControllerAdvice beans. #1452

smar21 opened this issue Aug 20, 2019 · 4 comments

Comments

@smar21
Copy link

smar21 commented Aug 20, 2019

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.

@ryanjbaxter
Copy link
Contributor

@dsyer @spencergibb any thoughts on this one?

@spencergibb
Copy link
Member

No idea, but I'd remove @RefreshScope from TestAdvice and move properties to a @ConfigurationProperties bean or some other bean that has @RefreshScope.

@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@smar21
Copy link
Author

smar21 commented Sep 30, 2019

Yes, that's what I did.

But I had to discover that myself; it's not documented anywhere that I could see...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants