Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doesn't autowiring limit goal of IoC?

IoC is nice but, used with autowiring (@EJB, @Autowired, @Inject, @SpringBean...), don't you think it limit a goal of IoC?

Actually i don't know so much about autowiring systems in different frameworks but it seems that it's mainly based on types.

When you use @EJB on IService then you need to have only one implementation ServiceImpl to make it work.

What if we want many implementations?

It seems some autowiring annotations can have arguments. For exemple in Stripes you can do: @SpringBean("xxxService") Where xxxService is a spring initialized bean.

In such a case, ok you don't do "new XxxServiceImpl()" But you still put a hardcoded reference to the service implementation you want to use in your bean. It's simply not a class reference but a spring bean reference to the implementation...

What do you think about that? I like autowiring but just wonder myself....

like image 698
Sebastien Lorber Avatar asked Dec 17 '10 14:12

Sebastien Lorber


People also ask

What does the Autowired annotation do?

Starting with Spring 2.5, the framework introduced annotations-driven Dependency Injection. The main annotation of this feature is @Autowired. It allows Spring to resolve and inject collaborating beans into our bean.

What is the difference between @autowired and @qualifier?

The difference are that @Autowired and @Qualifier are the spring annotation while @Resource is the standard java annotation (from JSR-250) . Besides , @Resource only supports for fields and setter injection while @Autowired supports fields , setter ,constructors and multi-argument methods injection.

Why Autowiring can't be used to inject primitive and string values?

Autowiring can't be used to inject primitive and string values. It works with reference only. Injected referance need to have a class which is object. you can use wrapper for same.

Which of the following is default Autowiring mode?

The default mode of the @Autowired is byType .


2 Answers

Yes, there are limitations to autowiring (just one implementation of an autowired interface), so it defeats a part of the IoC flexibility when it comes to injecting the proper implementation.

However, I see autowiring as just being a way to reduce configuration. So when 90% of your dependencies can be autowired, you tend to end up with less configuration overall (and the remaining configuration is significant, because it only contains the important (implementation-specific) bits)

like image 51
bug Avatar answered Sep 19 '22 18:09

bug


I doubt this can be considered an objective question, but let's have a shot at this.

I've had quite some discussions about this, and it's certainly not only you that's thinking this way.

Yes, doing it that way makes IoC somewhat pointless. It still makes it easier because you dont have to figure out the ideal wiring order yourself, but you lose the advantage of being able to switch implementations by changing a configuration file, which is one of the reasons we started with IoC in the first place.

There seem to be two main approaches to switching between implementations now:

  1. Use Qualifiers

You can add an annotation on your implementation, and another one on your injection point, which will tell the container which one you want to use. You still have to change your code in two spots though, so it's the same as implementing a sub-interface and wiring that by type. It's also still hard-coded.

  1. Use a beanconfigurer

Spring has this beanconfigurer concept, which just replaces the old xml files. You can handle the configuration in a certain class which will tell the container how to wire. I don't see the advantage over the old style (for this cause, xml syntax is more readable), but I guess it's a matter of taste.

For me, the only way to use autowiring by type in a decent way is to play with the classpath, so that you can pop in mocks instead of implementations by including a different class. But since java classpath has such a user-friendly interface, I also don't think this is easier than the old xml way of doing things.

So in the end, I think it all comes down to a matter of taste. Yes, autowiring using annotations is a lot easier, but it does hardcode your configuration into your code, just as you say. The question becomes, does it really change that often that it warrants a 'softcoding' approach?

like image 39
Joeri Hendrickx Avatar answered Sep 17 '22 18:09

Joeri Hendrickx