@Bean inside class with @Configuration and without it

后端 未结 2 842
天命终不由人
天命终不由人 2020-12-18 04:48

There is a @Bean annotation in Spring 3.0. It allows to define a Spring bean directly in a Java code. While browsing Spring reference I found two different ways

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 05:08

    @Bean [instance method] inside @Component - One method with @Bean instance call other method @Bean instance , then it would be simple java semantics call i.e. Object won't returned by Spring container , It would be normal return from java instance factory method,because Component class don't extends CGLIB.

    @Bean [instance method] inside @Configuration - In this case , spring container would be returning reference to exisiting object. It won't be normal java sematic call.

    @Bean on static method inside Configuration & Component Class - In this case , @Bean method would never be intercepted by the container neither in Configuration class nor in Component Sterotype class.

提交回复
热议问题