Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring not able to create bean

I am a beginner to spring and its concepts. I am trying to use @Configuration and package-scan annotations to scan for some bean provider classes under a single package. When the @Bean annotated method of one of the class is having the same name as another @Bean annotated method of a different class, bean creation for both the classes doesnt happen. If I change the @bean annotated method name to a different name for the bean which is not created, both beans are successfully created. Not able to understand this behaviour.

    @Configuration
    public class ManagementHelperProvider {
        @Bean
        public ManagementHelper getInstance() {
            return new ManagementHelper();
        }
    }

If I am creating another Class like below the top Bean ManagementHelper is not created.

    @Configuration
    public class ManagementValidatorProvider {
        @Bean
        public ManagementValidator getInstance() {
            return new ManagementValidator();
        }
    }

If I am creating another Class like below the top Bean ManagementHelper is created.

    @Configuration
    public class ManagementValidatorProvider {
        @Bean
        public ManagementValidator getInstanceTwo() {
            return new ManagementValidator();
        }
    }
like image 832
Aditya Raman Avatar asked Jan 29 '26 15:01

Aditya Raman


2 Answers

Case1:

bean1 created with the name getInstance.

bean2 created with the same name getInstance and bean1 was overridden by this.

Case2:

bean1 created with the name getInstance.

bean2 created with the name getInstanceTwo. No override, because no conflict in names.

If you

 @Bean(name="bean1") 

and

@Bean(name="bean2") 

it will also work.

@Configuration
public class AppConfig {
    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }
}

The above is exactly equivalent to the following appConfig.xml:

<beans>
    <bean name="transferService" class="com.acme.TransferServiceImpl"/>
</beans>

Both will result in a bean named transferService being available in the BeanFactory/ApplicationContext, bound to an object instance of type TransferServiceImpl:

transferService => com.acme.TransferService
like image 66
Sundararaj Govindasamy Avatar answered Jan 31 '26 05:01

Sundararaj Govindasamy


Since you are trying to override a bean it will throw exception

If still you want to do it.

Refer to setAllowBeanDefinitionOverriding - https://docs.spring.io/spring/docs/2.5.x/javadoc-api/org/springframework/beans/factory/support/DefaultListableBeanFactory.html#setAllowBeanDefinitionOverriding%28boolean%29

Or

You can simply change the name of the bean using name property

  • @Bean(name="name1")
  • @Bean(name="name2")
like image 41
Pankaj Kumar Avatar answered Jan 31 '26 04:01

Pankaj Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!