简体   繁体   中英

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();
        }
    }

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

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")

By default the beans in spring are singletons and only need to be defined once in the context. Component scanning will search all files.

You do not need to define the getInstance() multiple times, just once! For clarity rename this method like below:

@Bean
public ManagementHelper getManagementHelperInstance() {
    return new ManagementHelper();
}

Where you need to rely on it in other classes add the following in your class as a member variable

@Inject
ManagementHelper managementHelperInstance

If you don't want to use the same ManagementHelper in multiple places (not a singleton) then annotate the MangementHelper class as a prototype and this will create a new instance of the bean on every request, not just return the same one ie

@Scope("prototype")
public class ManagementHelper 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM