开发者

Why to use @Autowired on class in Spring

I read about the advantages of using Dependency for 开发者_JAVA百科interface.

I understand the concept for interface - but why to use @Autowire on class? If we use Autowire on class I know in advance what is the implmeneted class and it's like a regular member of it (without the ability of get to this member)!

What am I missing?


1) Convenience - you do not need to take care for initializing your components, you save time on typing code and configuration files,

2) Forcing good practices - your components to be autowired must be written to be manageable by Spring and spring will take care about error checking for you and pop all errors. So your code will be organized in component collaborating way.

3) Autowiring will also reduce your effort when your classes/beans will grow and evolve.


If you use @Autowire and not call the constructor, you mark the class to be dynamically initialized by the Spring Container. This allows you to set class properties as defined in your spring configuration.


If we use Autowire on class I know in advance what is the implmeneted class and it's like a regular member of it

When you wire the dependency through XML instead of Annotations, you also know in advance in which class are you going to inject it. But You still declare an Interface as dependency, so you can wire any Implementation of this interface at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜