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

Map injection doesn't honor @Order #34688

Closed
quaff opened this issue Mar 31, 2025 · 1 comment
Closed

Map injection doesn't honor @Order #34688

quaff opened this issue Mar 31, 2025 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another

Comments

@quaff
Copy link
Contributor

quaff commented Mar 31, 2025

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

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig
class OrderConsistencyTests {

	@Test
	void test(@Autowired Map<String, Service> serviceMap,
			@Autowired List<Service> serviceList) {
		assertThat(serviceMap.values()).containsExactlyElementsOf(serviceList);
	}

	static class Service implements BeanNameAware {

		private String beanName;

		@Override
		public void setBeanName(String name) {
			this.beanName = name;
		}

		@Override
		public String toString() {
			return beanName;
		}
	}

	@Configuration
	static class Config {

		@Bean
		Service service1() {
			return new Service();
		}

		@Bean
		@Order(Ordered.LOWEST_PRECEDENCE - 1)
		Service service2() {
			return new Service();
		}

		@Bean
		@Order(Ordered.LOWEST_PRECEDENCE - 2)
		Service service3() {
			return new Service();
		}
		
		@Bean
		@Order(Ordered.LOWEST_PRECEDENCE - 3)
		Service service4() {
			return new Service();
		}
		
		@Bean
		@Order(Ordered.LOWEST_PRECEDENCE - 4)
		Service service0() {
			return new Service();
		}

	}

}
org.opentest4j.AssertionFailedError: 
expected: [service0, service4, service3, service2, service1]
 but was: [service1, service2, service3, service4, service0]
	at example.OrderConsistencyTests.test(OrderConsistencyTests.java:28)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

I'm not saying it's a bug because the Javadoc of @Order mentions collection but not map injection, since the serviceMap is a LinkedHashMap not a HashMap, I think it's better to keep the same order, but my test shows the order is declaration order.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 31, 2025
@sdeleuze sdeleuze changed the title Map injection doesn't honer @Oder Map injection doesn't honor @Order Mar 31, 2025
@sdeleuze sdeleuze added the in: core Issues in core modules (aop, beans, core, context, expression) label Mar 31, 2025
quaff added a commit to quaff/spring-framework that referenced this issue Apr 2, 2025
Fix spring-projectsGH-34688

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
quaff added a commit to quaff/spring-framework that referenced this issue Apr 2, 2025
Fix spring-projectsGH-34688

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
@sbrannen
Copy link
Member

sbrannen commented Apr 2, 2025

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Apr 2, 2025
@sbrannen sbrannen added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 2, 2025
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: superseded An issue that has been superseded by another
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants