Issue
Is there any difference in setting a name to a bean while using value or name method of bean annotation.
@Configuration
public class OperationConfig {
@Bean(name = "test")
public IOperation getOperation()
{
return new IntMultiplyOperation();
}
}
or
@Configuration
public class OperationConfig {
@Bean("test")
public IOperation getOperation()
{
return new IntMultiplyOperation();
}
}
Spring can catch both of them with this constructor
@Configuration
public class ApplicationRunnerConfig {
private final IOperation m_operation;
public ApplicationRunnerConfig(@Qualifier("test") IOperation operation)
{
m_operation = operation;
}
}
Solution
Annotation without any field reference maps to value attribute and as in @Bean annotation value is alias for name both ways are identical.
Check for more information
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html
Answered By - DhruvG
Answer Checked By - Marie Seifert (JavaFixing Admin)