-
Notifications
You must be signed in to change notification settings - Fork 38k
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
Bean overriding for 'taskExecutor' alias is not detected #25430
Comments
UPDATE Looks the problem is with the class name, by changing it to Works
Doesn't work
|
@rgampa there is now a
I don't think that is possible as we do fail if something is trying to override a bean by name the way I think this is happening here. If you have set this property yourself, it is more than a simple upgrade as you described it. Getting this failure and taking appropriate action is what you should do on upgrade. If you haven't set |
@snicoll Thanks for your reply. I would have checked existing bean before posting here, but error was confusing. I haven't set |
@snicoll Hi. I update a project to reproduce this problem. here 's the link : https://github.com/Pinocchio2018/TaskExecutorBeanProblem |
Thank you for sharing a sample.
The problem is described here already. Your user-bean has the same name as a (new) well-known component in Spring Boot. The auto-configuration for task execution will unfortunately create a bean with id In the meantime you can change the name of your bean, for instance: @Service("myTaskExecutor")
public class TaskExecutor { That said, I would expect your project to fail differently with a nicer exception message as we've disabled bean overriding by default. But it doesn't and I can't see why that is. |
This looks like a Framework bug to me. Here's a minimal sample that reproduces some behaviour that I would not expect: package com.example;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
public class TaskExecutorBeanProblemApplication {
public static void main(String[] args) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();
beanFactory.setAllowBeanDefinitionOverriding(false);
context.register(FirstConfiguration.class, SecondConfiguration.class);
context.refresh();
System.out.println(context.getBean("taskExecutor"));
System.out.println(beanFactory.getBeanDefinition("taskExecutor"));
}
}
@Configuration
static class FirstConfiguration {
@Bean
TaskExecutor taskExecutor() {
return new TaskExecutor();
}
}
@Configuration
static class SecondConfiguration {
@Bean(name = { "applicationTaskExecutor", "taskExecutor" })
ThreadPoolTaskExecutor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
}
static class TaskExecutor {
}
} The
While the
I would guess that this mismatched view of the state is why the override isn't detected and prevented. |
I think this may be a bug. I check the // Consider name and any aliases
List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name")));
String beanName = (!names.isEmpty() ? names.remove(0) : methodName);
// Register aliases even when overridden
for (String alias : names) {
this.registry.registerAlias(beanName, alias);
}
// Has this effectively been overridden before (e.g. via XML)?
if (isOverriddenByExistingDefinition(beanMethod, beanName)) {
......
} It uses the first item in the name array as the beanName, therefore, when checking whether the existing bean definition can be overridden, it is actually not checked whether the alias exists In order to verify, I changed the name of the custom bean from TaskExecutor to ApplicationTaskExecutor. After executing it again, it reminded me that the name has been used. This is different from the previous bean not found error When verifying whether the bean can be overridden, I think both beanName and its alias should be checked |
This seems to be caused by a missing check for an alias overriding a bean definition of the same name in I'll make sure to get this into 5.3 M2. Backporting is tough here since some applications may rely on an accidental override. |
Earlier I was using 2.0.9.RELEASE, i have simply upgraded to 2.1.7.RELEASE. One of the bean couldn't be found or scanned by ComponentScan.
In main class
@ComponentScan
is present and TaskExecutor is annotated with@Component
.I have tried moving up the main class, but didn't help still.
The text was updated successfully, but these errors were encountered: