使用Spring直接连线具体课程 [英] Using Spring to wire directly a concrete class

查看:75
本文介绍了使用Spring直接连线具体课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring中使用@Autowired直接连接到具体类而不是接口(并使用按类型"自动装配)是否有意义

Does it makes sense in Spring to use @Autowired to wire directly to a concrete class and not to an interface (and make use of 'by type' autowiring)

如果一个类没有实现一个接口,最好不要通过构造函数或工厂实例化它(保持简单);而不是仅仅为了它而使它成为Spring bean.

If a class doesn't implements an interface wouldn't it be better to instantiate it via constructor or a factory (keeping things simple); rather than make it a Spring bean just for the heck of it.

推荐答案

在Spring中使用@Autowired直接连接到具体类而不是接口是否有意义

Does it makes sense in Spring to use @Autowired to wire directly to a concrete class and not to an interface

好的.自动装配的做法与您要自动装配的什么无关.它将与类以及接口一起使用.

Sure. The practice of autowiring is independent of what you're autowiring. It'll work with classes just as well as interfaces.

尽管这是一个更普遍的问题,即您是否应该始终为给定类引入接口,而不是直接与类类型对话,但这是一个好主意,这仍有待商bat.好处包括更容易的单元测试和更简洁的设计,但以代码混乱为代价.

However, whether or not it's a good idea is debatable, although this is a more general question of whether you should always introduce an interface for a given class, rather than talk directly to the class type. The benefits include easier unit-testing and a cleaner design, at the expense of code clutter.

自动连接接口类型而不是类类型还有一个很好的理由,那就是如果Spring在注入之前需要在bean周围生成代理对象,那么如果bean的类定义了 any 接口,那么代理将实现这些接口,并且与Bean类本身类型兼容.如果然后尝试按类类型自动装配该bean,它将失败.避免这种烦人的情况的最简单方法是始终按接口类型自动接线,这样它将始终如您所愿.

There's another good reason to autowire interface types rather than class types, which is that if Spring needs to generate a proxy object around the bean before injecting it, then if the bean's class defines any interfaces, then the proxy will implement those interfaces, and will not be type-compatible with the bean class itself. If you then try and autowire that bean by class type, it will fail. The easiest way to avoid this annoying scenario is to always autowire by interface type, that way it will lways work as you expect.

并利用按类型"自动接线

and make use of 'by type' autowiring

如果您是指容器-级别byType自动装配,那么您不想这样做.这是旧的Spring 1.x自动装配样式,并且非常灵活(请参阅

If you mean container-level byType autowiring, then you don't want to do that. It's the old Spring 1.x style of autowiring, and it's highly inflexibile (see limitations of autowiring).

@Autowired保持一致,它更加灵活且易于控制.

Stick with @Autowired, it's much more flexible and easier to control.

如果一个类没有实现一个接口,最好不要通过构造函数或工厂实例化它(保持简单);而不是仅仅为了它而使它成为Spring bean.

If a class doesn't implements an interface wouldn't it be better to instantiate it via constructor or a factory (keeping things simple); rather than make it a Spring bean just for the heck of it.

这两个问题是完全分开的.如果您需要Spring来控制对象的依赖关系和生命周期,则无论该对象是否实现接口,都应将其制成Spring Bean.如果您发现该对象没有依赖项,也没有有意义的接口,那么也许没有理由将其设为bean.

The two questions are completely separate. An object should be made a Spring bean if you need Spring to control its dependencies and lifecycle, regardless of whether or not it implements interfaces. If you find that the object has no dependencies, and no meaningful interface, then perhaps there is no reason to make it a bean.

这篇关于使用Spring直接连线具体课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆