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....
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.
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.
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.
The default mode of the @Autowired is byType .
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)
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:
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With