我希望能够自动连接单例bean(foo)
foo
@Component public class FooUser { @Autowire Foo foo; }
由另一个单例方法(FooFactory.createFoo)创建
FooFactory.createFoo
@Service public class FooFactory { public Foo createFoo() {...} }
使用xml就是factory-method。我该如何使用注释?
factory-method
尝试使用Java @Configuration:
@Configuration:
@Configuration public class Config { @Bean public FooUser fooUser() { return new FooUser(foo()); } @Bean public FooFactory fooFactory() { return new FooFactory(); } @Bean public Foo foo() { return fooFactory().createFoo(); } }