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

Skip overriding primary bean definition with non-primary one #32125

Closed
quaff opened this issue Jan 26, 2024 · 4 comments
Closed

Skip overriding primary bean definition with non-primary one #32125

quaff opened this issue Jan 26, 2024 · 4 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@quaff
Copy link
Contributor

quaff commented Jan 26, 2024

Here is my case, I defined a default bean in xml configuration, sometimes I want override that bean with another implementation via @Component and @Primary, it's fine if injection is autowired by type but not name, the ideal solution is pinning the primary bean definition make it cannot be overridden by non-primary one.

Here is test case:

package test;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig
@ContextConfiguration(locations = "ctx.xml")
public class OverrideBeanTests {

	@Autowired
	private Service service;

	@Test
	void test() {
		assertThat(service).isInstanceOf(CustomService.class);
	}

}
package test;

public class Service {

}
package test;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Primary
@Component("service")
public class CustomService extends Service {

}
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	https://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	https://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="test"/>
	<bean id="service" class="test.Service"/>
</beans>
quaff added a commit to quaff/spring-framework that referenced this issue Jan 26, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 26, 2024
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Jan 26, 2024
@sbrannen
Copy link
Member

The appears to be related to:

@quaff
Copy link
Contributor Author

quaff commented Jan 26, 2024

The appears to be related to:

That cannot handle autowiring by name usage, and multiple instances are created but only one is needed.

@snicoll
Copy link
Member

snicoll commented Jan 26, 2024

I understand that the behavior can be rather unintuitive but we've decided no to make further changes in that area. Actually, we're going to deprecate bean definition overriding wholesale, see #31288.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2024
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 26, 2024
@quaff
Copy link
Contributor Author

quaff commented Jan 26, 2024

I understand that the behavior can be rather unintuitive but we've decided no to make further changes in that area. Actually, we're going to deprecate bean definition overriding wholesale, see #31288.

It make sense that primary bean definition override non-primary one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants