在Spring将要连接的类中使用@Autowired的优缺点是什么?
为了澄清一下,我特别讨论的是@Autowired注释,而不是XML中的自动连接。
我可能只是不理解它,但对我来说,这似乎是一种反模式——您的类开始意识到它们被绑定到DI框架上,而不仅仅是pojo。也许我是一个贪吃惩罚的人,但是我喜欢为bean使用外部XML配置,而且我喜欢显式连接,这样我就确切地知道在哪里连接了什么。
在Spring将要连接的类中使用@Autowired的优缺点是什么?
为了澄清一下,我特别讨论的是@Autowired注释,而不是XML中的自动连接。
我可能只是不理解它,但对我来说,这似乎是一种反模式——您的类开始意识到它们被绑定到DI框架上,而不仅仅是pojo。也许我是一个贪吃惩罚的人,但是我喜欢为bean使用外部XML配置,而且我喜欢显式连接,这样我就确切地知道在哪里连接了什么。
当前回答
我对这个主题的看法是,xml配置降低了代码的清晰度,特别是在大型系统中。
Annotations like @Component makes things even worse. It steers developers to make objects mutable, as dependencies can't be made final anymore, given that default constructors need to be provided. Dependencies need to be either injected through public setter, or uncontrolled through @Autowired. [even worse dependency injection is compromised with classes that instantiate their dependencies, I still see this in newly written code!]. By uncontrolled I mean, in large systems, when multiple implementations (or children) of the type are available, it gets much more involved to understand which of the implementations was @Autowired, a complexity that makes investigating bugs much harder. It also means that, supposedly you have a profile for test environment and another for production, your production bugs will only happen when it hurts most - in production, rather than being able to spot the bugs in the test environment, or even better, at compile time!
我坚持中间立场,在那里我声明我的配置类(es),(基于java的Spring配置使用@Configuration)
我在配置类中显式地声明了所有bean。我只在配置类中使用@Autowired,目的是限制Spring对配置类的依赖。
@Configuration位于一个特定的包中,这是spring扫描运行的唯一位置。(这大大加快了大型项目的启动时间)
我努力使我的所有类都是不可变的,尤其是数据对象、JPA、Hibernate和Spring,以及许多序列化库似乎都破坏了这一点。我避免任何强迫我提供setter或从属性声明中删除最后一个关键字的操作。
减少对象创建后更改的可能性,大大减少了大型系统中的bug,并减少了在存在bug时查找bug的时间。
它似乎还迫使开发人员更好地设计系统不同部分之间的交互。问题和bug变成越来越多的编译错误,这减少了浪费的时间,提高了工作效率。
其他回答
我真的很喜欢用注释来写,而不是XML。 根据Spring手册和最新版本,XML和Annotation实现了相同的结果。
这是我的清单
Pro:
从xml中删除无用的行 简化代码调试:当您打开一个类时,您可以读取类中的内容 更快速的开发,具有400行或更多XML的项目是可读的?
缺点:
不是标准的Java实现,但您可以切换到使用@Inject,这是一个Java标准Api,因此bean仍然是Pojo 你不能简单地到处使用,db连接等等,但这只是一个观点,我更喜欢有一个地方读取所有配置。
关于切换环境的讨论很少。在我参与过的大多数项目中,根据我们所工作的环境注入依赖项是一个真正的问题。使用Spring EL的xml配置非常简单,而且我不知道任何带有注释的好解决方案。我刚刚发现了一个问题:
@Value("#{${env} == "production" ? realService : dummyService}")
private SomeService service;
它应该会起作用,但不是一个很好的解决方案。
根据我的理解,@Autowired是最好的使用,同时参考接口引用和使用它的覆盖函数,但我只发现问题是,它有时在运行时被分配为null。
以下是一些经验 优点
使配置更容易,因为我们可以只使用@Autowire注释 不想使用setter方法,这样类会更干净
Cons
紧密耦合到xml文件,即使我们使用DI 很难找到实现(但如果你使用像intellij这样的好ide,你肯定可以摆脱这个)
就我个人经验而言,除了在测试用例中,我很少使用@AutoWire注释。
我对这个主题的看法是,xml配置降低了代码的清晰度,特别是在大型系统中。
Annotations like @Component makes things even worse. It steers developers to make objects mutable, as dependencies can't be made final anymore, given that default constructors need to be provided. Dependencies need to be either injected through public setter, or uncontrolled through @Autowired. [even worse dependency injection is compromised with classes that instantiate their dependencies, I still see this in newly written code!]. By uncontrolled I mean, in large systems, when multiple implementations (or children) of the type are available, it gets much more involved to understand which of the implementations was @Autowired, a complexity that makes investigating bugs much harder. It also means that, supposedly you have a profile for test environment and another for production, your production bugs will only happen when it hurts most - in production, rather than being able to spot the bugs in the test environment, or even better, at compile time!
我坚持中间立场,在那里我声明我的配置类(es),(基于java的Spring配置使用@Configuration)
我在配置类中显式地声明了所有bean。我只在配置类中使用@Autowired,目的是限制Spring对配置类的依赖。
@Configuration位于一个特定的包中,这是spring扫描运行的唯一位置。(这大大加快了大型项目的启动时间)
我努力使我的所有类都是不可变的,尤其是数据对象、JPA、Hibernate和Spring,以及许多序列化库似乎都破坏了这一点。我避免任何强迫我提供setter或从属性声明中删除最后一个关键字的操作。
减少对象创建后更改的可能性,大大减少了大型系统中的bug,并减少了在存在bug时查找bug的时间。
它似乎还迫使开发人员更好地设计系统不同部分之间的交互。问题和bug变成越来越多的编译错误,这减少了浪费的时间,提高了工作效率。