Reject bean names with factory prefix for Bean Overrides

Closes gh-33674
This commit is contained in:
Sam Brannen
2024-10-09 14:14:29 +02:00
parent c864afd6fe
commit b5c82b8dcb
2 changed files with 35 additions and 12 deletions

View File

@@ -56,6 +56,18 @@ import static org.mockito.Mockito.mock;
*/
class BeanOverrideBeanFactoryPostProcessorTests {
@Test
void beanNameWithFactoryBeanPrefixIsRejected() {
AnnotationConfigApplicationContext context = createContext(FactoryBeanPrefixTestCase.class);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
Unable to override bean '&messageService' for field 'FactoryBeanPrefixTestCase.messageService': \
a FactoryBean cannot be overridden. To override the bean created by the FactoryBean, remove the \
'&' prefix.""");
}
@Test
void replaceBeanByNameWithMatchingBeanDefinition() {
AnnotationConfigApplicationContext context = createContext(CaseByName.class);
@@ -348,6 +360,18 @@ class BeanOverrideBeanFactoryPostProcessorTests {
}
@FunctionalInterface
interface MessageService {
String getMessage();
}
static class FactoryBeanPrefixTestCase {
@DummyBean(beanName = "&messageService")
MessageService messageService;
}
static class CaseByName {
@DummyBean(beanName = "descriptionBean")
@@ -464,11 +488,6 @@ class BeanOverrideBeanFactoryPostProcessorTests {
}
}
@FunctionalInterface
interface MessageService {
String getMessage();
}
static class MessageServiceTestCase {
@TestBean(name = "messageServiceBean")