annotation based factory methods

后端 未结 5 421
無奈伤痛
無奈伤痛 2020-12-11 15:24

I want to be able to autowire a singleton bean (foo)

@Component
public class FooUser {

  @Autowire Foo foo;
}

created by ano

5条回答
  •  星月不相逢
    2020-12-11 15:51

    Spring Components can also define factory methods. A snipped from the documentation:

    @Component
    public class FactoryMethodComponent {
    
      @Bean @Qualifier("public")
      public TestBean publicInstance() {
          return new TestBean("publicInstance");
      }
    
      public void doWork() {
          // Component method implementation omitted
      }
    }
    

提交回复
热议问题