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

Failure analysis of NoUniqueBeanDefinitionException reports "defined in null" when bean definition has no resource description #33765

Closed
sergey-morenets opened this issue Jan 11, 2023 · 3 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@sergey-morenets
Copy link

sergey-morenets commented Jan 11, 2023

If I try to declare the same class twice as Spring bean and then inject it:

@Component
public class MemoryWriter {}

and

public class AppInitializer implements ApplicationContextInitializer<GenericApplicationContext>{

	@Override
	public void initialize(GenericApplicationContext ctx) {
		ctx.registerBean(MemoryWriter.class);
	}	
}

then Spring Boot prints out an error and that's correct but in the second case it doesn't specify location of the class where bean is declared (just "null"):

Parameter 0 of method server in demo.AppConfig required a single bean, but 2 were found:
	- demo.MemoryWriter: defined in null
	- memoryWriter: defined in file [c:\demo\target\classes\demo\MemoryWriter.class]

Spring Boot: 3.0.1

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 11, 2023
@wilkinsona
Copy link
Member

The null is for the bean that was registered in the ApplicationContextInitializer. Without walking the stack, which is prohibitively slow, there's no way for the application context to know where the bean was defined. With this in mind, what would you expect the error message to say?

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jan 12, 2023
@sergey-morenets
Copy link
Author

Hi @wilkinsona

Well, you're Spring developer so it's up to you to make decisions on that.
The message text can be "defined in unknown sources" or "defined in the application context (probably using ApplicationContextInitializer)".
But I wouldn't agree about speed. This incorrect configuration would lead to application shutdown so additional milliseconds to find cause of the issue don't matter.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 12, 2023
@philwebb philwebb changed the title "defined in null" error message InjectionFailureAnalyzers reports "defined in null" message BeanDefinition.getResourceDescription is unknown Jan 12, 2023
@philwebb
Copy link
Member

This incorrect configuration would lead to application shutdown so additional milliseconds to find cause of the issue don't matter.

I don't think we can capture the location once the application has failed, I think we'd need to capture it when the bean is defined. This would have a negative impact of successfully started apps as well.

The message text can be "defined in unknown sources"

This seems like good suggestions. We can detect null and replace it with that message.

@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jan 12, 2023
@philwebb philwebb added this to the 2.7.x milestone Jan 12, 2023
@wilkinsona wilkinsona changed the title InjectionFailureAnalyzers reports "defined in null" message BeanDefinition.getResourceDescription is unknown Failure analysis of NoUniqueBeanDefinitionException reports "defined in null" when bean definition has no resource description Jan 18, 2023
@wilkinsona wilkinsona self-assigned this Jan 18, 2023
@wilkinsona wilkinsona modified the milestones: 2.7.x, 2.7.8 Jan 18, 2023
krenson pushed a commit to krenson/test-push that referenced this issue Mar 15, 2023
…ot-starter-parent from 2.7.7 to 2.7.8 (patch)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [org.springframework.boot:spring-boot-starter-parent](https://spring.io/projects/spring-boot) ([source](https://github.com/spring-projects/spring-boot)) | parent | patch | `2.7.7` -> `2.7.8` |

---

### Release Notes

<details>
<summary>spring-projects/spring-boot</summary>

### [`v2.7.8`](https://github.com/spring-projects/spring-boot/releases/tag/v2.7.8)

[Compare Source](spring-projects/spring-boot@v2.7.7...v2.7.8)

#### ⭐ Noteworthy

-   The coordinates of the MySQL JDBC driver have [changed from `mysql:mysql-connector-java` to `com.mysql:mysql-connector-j`](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#mysql-jdbc-driver).

#### 🐞 Bug Fixes

-   Devtools sets non-existent property spring.reactor.debug [#&#8203;33858](spring-projects/spring-boot#33858)
-   Failing calls to reactive health indicators are not logged [#&#8203;33774](spring-projects/spring-boot#33774)
-   Failure analysis of NoUniqueBeanDefinitionException reports "defined in null" when bean definition has no resource description [#&#8203;33765](spring-projects/spring-boot#33765)
-   NPE in RabbitProperties when user is given, but password not [#&#8203;33752](spring-projects/spring-boot#33752)
-   SDKMAN should not use repo.spring.io for releases [#&#8203;33708](spring-projects/spring-boot#33708)
-   Homebrew and Scoop should not use repo.spring.io for releases [#&#8203;33702](spring-projects/spring-boot#33702)
-   EndpointRequestMatcher should have a toString method [#&#8203;33690](spring-projects/spring-boot#33690)
-   It is not possible to provide a custom TransactionProvider bean for JOOQ [#&#8203;32899](spring-projects/spring-boot#32899)
-   SpringBootMockResolver causes AopTestUtils.getUltimateTargetObject to recurse until the stack overflows when it calls it with Spring Security's authentication manager bean [#&#8203;32632](spring-projects/spring-boot#32632)
-   Inconsistent discovery of parameter names for selectors in custom actuator endpoints [#&#8203;31240](spring-projects/spring-boot#31240)
-   `@DeprecatedConfigurationProperty` has no effect when declared on a record component's accessor method [#&#8203;29526](spring-projects/spring-boot#29526)
-   Headless mode is forced when banner.\* file is present. [#&#8203;28803](spring-projects/spring-boot#28803)
-   Diagnostics are poor when the JMX port used by the Maven start goal is in use [#&#8203;24044](spring-projects/spring-boot#24044)

#### 📔 Documentation

-   Replace "via" in documentat...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants