Apache Camel 与构造函数注入
我最近加入了一个大量使用 Apache Camel 的项目。我看到很多代码看起来像这样:
@Service
public class MyClass
{
public MyClass()
{
myOtherClass= new MyOtherClass();
}
...
private MyOtherClass myOtherClass;
}
这让我很痛苦,因为我讨厌在构造函数中 new
一个对象。这使得单独测试该类变得困难。我更喜欢使用构造函数注入的形式。
换句话说,我希望能够将对 MyOtherClass 的引用传递到 MyClass 的构造函数中。这可能吗?
请记住,我们正在使用 Camel。这是Camel的服务,我的理解是Camel负责赋予MyClass“生命”。
欢迎所有建议。谢谢!
I recently joined a project that makes heavy usage of Apache Camel. I see a lot of code that looks like this:
@Service
public class MyClass
{
public MyClass()
{
myOtherClass= new MyOtherClass();
}
...
private MyOtherClass myOtherClass;
}
This pains me, because I hate to new
an object inside the constructor. It makes it difficult to test the class in isolation. I would much prefer to use a form of constructor injection.
In other words, I would like to be able to pass a reference to MyOtherClass into the constructor of MyClass. Is this possible?
Bear in mind that we're using Camel. This is a Camel service, and my understanding is that Camel is responsible for giving MyClass "life".
All recommendations welcome. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Camel 中没有 @Service 注释,因此它是由其他框架执行此操作的。你用的是Spring吗?然后你可以使用 spring 的方式来做到这一点。
There is no @Service annotation in Camel, so its some other framework doing this. Are you using Spring? Then you can use the spring ways of doing this.