Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个服务 X 和 Y。如果我想在 X 中调用 Y。有没有办法通过注释来做到这一点。我不想为 X/Y 配置 bean,因为所有其他资源都是为 X 自动装配的。
谢谢!
Spring 只能注入托管实例:
@Service public class X { @Resource private Y y; } @Service public class Y { }
如果您不喜欢将@Service 添加到类 Y,那么您可以使用它(X 将是相同的)
@Configuration public class AppConfig { @Bean public Y y { return new Y(); } }