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

Multiple @ServletComponentScan with no base packages may cause startup to fail with an UnsupportedOperationException #12715

Closed
wants to merge 2 commits into from

Conversation

vivimice
Copy link
Contributor

@vivimice vivimice commented Apr 2, 2018

#12714

Description

When a project contains multiple @ServletComponentScan annotated class in classpath, and at least one annotation don't explicitly specify basePackages and basePackageClass attribute, the application MAY
fail on startup, caused by UnsupportedOperationException:

java.lang.UnsupportedOperationException: null
	at java.util.AbstractCollection.add(AbstractCollection.java:262)
	at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
	at org.springframework.boot.web.servlet.ServletComponentScanRegistrar.updatePostProcessor(ServletComponentScanRegistrar.java:62)
	at org.springframework.boot.web.servlet.ServletComponentScanRegistrar.registerBeanDefinitions(ServletComponentScanRegistrar.java:48)
	...

Reproduction

There is a single class application to reproduce this bug just for demostration.

@SpringBootApplication
public class SpringBootTest1 {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootTest1.class, args);
    }
    
    @ServletComponentScan
    public static class Config1 {
        
    }
    
    @ServletComponentScan
    public static class Config2 {
        
    }
}

vivimice added 2 commits April 2, 2018 11:56
# Description

When a project contains multiple `@ServletComponentScan` annotated class in classpath, and at least one annotation don't explicitly specify `basePackages` and `basePackageClass` attribute, the application MAY
fail on startup, caused by UnsupportedOperationException:

<pre>
java.lang.UnsupportedOperationException: null
	at java.util.AbstractCollection.add(AbstractCollection.java:262)
	at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
	at org.springframework.boot.web.servlet.ServletComponentScanRegistrar.updatePostProcessor(ServletComponentScanRegistrar.java:62)
	at org.springframework.boot.web.servlet.ServletComponentScanRegistrar.registerBeanDefinitions(ServletComponentScanRegistrar.java:48)
	...
</pre>

# Reproduction

There is a single class application to reproduce this bug just for demostration.

<pre>
@SpringBootApplication
public class SpringBootTest1 {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootTest1.class, args);
    }
    
    @ServletComponentScan
    public static class Config1 {
        
    }
    
    @ServletComponentScan
    public static class Config2 {
        
    }
}
</pre>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 2, 2018
@vivimice vivimice changed the title #12714 UnsupportedOperationException when startup Fix UnsupportedOperationException on startup Apr 2, 2018
@philwebb philwebb added for: merge-with-amendments Needs some changes when we merge and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 2, 2018
@philwebb philwebb added this to the 1.5.x milestone Apr 2, 2018
@philwebb
Copy link
Member

philwebb commented Apr 2, 2018

We should ideally add a unit test for this.

@wilkinsona wilkinsona changed the title Fix UnsupportedOperationException on startup Multiple @ServletComponentScan with no base packages may cause startup to fail with an UnsupportedOperationException Apr 3, 2018
@wilkinsona wilkinsona added the type: bug A general bug label Apr 3, 2018
@wilkinsona wilkinsona self-assigned this Apr 3, 2018
wilkinsona pushed a commit that referenced this pull request Apr 3, 2018
Previously, when a project contained multiple `@ServletComponentScan`
annotated classes in classpath, and at least one annotation don't
explicitly specify `basePackages` and `basePackageClass` attribute,
the application could fail to start with an
UnsupportedOperationException. The failure occurred due to the
creating of an unmodifiable set when no base packages are configured
and a subsequent attempt to add base packages to that sit.

This commit fixes the issue by removing the use of an unmodifiable set
when `@ServletComponentScan` with no base packages in processed before
any other `@ServletComponentScan` annotations.

See gh-12715
@wilkinsona wilkinsona closed this in 6078fda Apr 3, 2018
wilkinsona added a commit that referenced this pull request Apr 3, 2018
* gh-12715:
  Polish "Use modifiable set for @ServletComponentScan with no packages"
  Use modifiable set for @ServletComponentScan with no packages
@wilkinsona
Copy link
Member

@vivimice Thank you very much for making your first contribution to Spring Boot. This fix that you proposed is now in 1.5.x and master. I added some tests in this polishing commit.

@wilkinsona wilkinsona modified the milestones: 1.5.x, 1.5.11 Apr 3, 2018
@vivimice
Copy link
Contributor Author

vivimice commented Apr 4, 2018

@wilkinsona My pleasure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: merge-with-amendments Needs some changes when we merge type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants