Does IOC target outsourcing?

IoC is good, but used with autowiring (@EJB, @Autowired, @Inject, @SpringBean ...), don't you think that it limits the purpose of IoC?

Actually, I don’t know much about the auto-subsystem in different frameworks, but it seems to be mainly based on types.

When you use @EJB in an IService, you only need to have one ServiceImpl implementation to make it work.

What if we need many implementations?

It seems that some auto-negotiation annotations may have arguments. For example, in Stripes you can: @SpringBean ("xxxService") Where xxxService is a spring initialized bean.

In that case, well, you are not making a “new XxxServiceImpl ()”, but you are still binding a hard link to the service implementation that you want to use in your bean. This is simply not a class reference, but a spring bean reference to an implementation ...

What do you think about this? I like the car shop, but just wondering myself ....

+7
source share
4 answers

Yes, there are restrictions on auto-installation (there is only one implementation of the auto-tuning interface), so it strikes some of the flexibility of IoC when it comes to entering the correct implementation.

However, I see that autowiring is just a way to reduce configuration. Therefore, when 90% of your dependencies can be auto-updated, you usually find yourself in a smaller configuration (and the rest of the configuration is significant, since it contains only important (implementation-specific) bits)

+6
source

I doubt that this can be considered an objective issue, but let's laugh at it.

I had quite some discussion about this, and this, of course, wasn’t just what you thought.

Yes, this makes IoC somewhat pointless. This still makes the job easier because you don’t need to determine the ideal wiring order yourself, but you lose the advantage of switching implementations by modifying the 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:

  • Use qualifiers

You can add an annotation to your implementation, and another to the injection point, which will tell the container you want to use. You should still change your code in two places, but in the same way as implement the sub-interface and wiring, which are by type. It is also still hardcoded.

  • Use beanconfigurer

Spring has this beanconfigurer concept, which simply replaces the old xml files. You can handle the configuration in a specific class that will tell the container how to connect. I don’t see an advantage over the old style (for this reason, the xml syntax is more readable), but I think this is a matter of taste.

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

So, in the end, I think it comes down to taste. Yes, auto-negotiation with annotations is a lot easier, but it hardcodes your configuration into your code, as you say. The question becomes, does it really change that often it requires a soft coding approach?

+2
source

In response to comments - an example of semantic names bean instead of "usedService" and "notUsedService". The general system will have some data pending between the servers, but some of them are stored only in one place.

@Autowired @Qualifier("customerDataSource") DataSource ds; @Autowired @Qualifier("geoipDataSource") DataSource geoip; 

and in xml:

 <bean id="customerDataSource" class="...ShardedDataSource"> <property name="dataSources"> <list> <value><ref local="ds1"/> <value><ref local="ds2"/> </list> </property> </bean> <bean id="ds1" class="...DataSource"> ... </bean> <bean id="ds2" class="...DataSource"> ... </bean> <alias name="ds2" alias="geoipDataSource"/> 
+2
source

You can try autowiring by name . This will solve your problem.

If you have a strict requirement for auto-preparation by type. Then you can indicate how it is,

 <bean id="PersonBean1" class="com.mkyong.common.Person"> <property name="name" value="mkyong1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> <bean id="PersonBean2" class="com.mkyong.common.Person"> <property name="name" value="mkyong2" /> <property name="address" value="address 2" /> <property name="age" value="28" /> </bean> 

and you can specify a bean in your class, for example,

 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Customer { @Autowired @Qualifier("PersonBean1") private Person person; private int type; private String action; //getter and setter methods } 

Hope this solves your problem.

0
source

All Articles