2

In all the past projects i have worked till now , beans are declared through XML bean configuration file for DI.

But there is another way which saves us from verbose configuration i.e auto scan(i.e <context:component-scan> .. </context:component-scan>), which detect and instantiate our beans from pre-defined project package, no more tedious beans declaration in in XML file.

So i am sure there must be some downside of auto scan approach. So any comments when we should go for auto scan and when not?

M Sach
  • 33,416
  • 76
  • 221
  • 314

4 Answers4

0

Beans which are supposed to be auto-detected by Spring, are considered as auto-configured. And there is an exact one to one mapping between annotated class (@Component, @Service, etc) and the bean, so you are very limited of controlling the wiring of the bean.

On the other side, beans defined manually (e.g. @Bean), are supposed to have a need of some configuration. There you explicitly define a single bean, instead of letting Spring container to do it automatically. One important thing is that it decouples the bean from the class definition, so you have a chance to configure bean as you want, and handle in a custom way.

So those two are really different things.

That said, if you don't need any specific/custom configuration, initialization of the bean - go with annotating the class, and let Spring do stuff for you by auto-scanning it.

If your bean needs to be configured, initialized in a custom way - declare it as a bean.

vtor
  • 8,989
  • 7
  • 51
  • 67
0

This question has asked previously. Look at this question.

Though here is something more.

Annotations have a lot of advantages over XML, to name a few :

  • Static type checking - the compiler will check for you where the annotation (once defined properly) is applicable and how
  • Clean code - its much easier to see (visually) the meta data defined in annotations

However it comes at a price: XML doesn't require recompilation when you want to change something. With annotation you'll have to recompile.

Community
  • 1
  • 1
ersnh
  • 522
  • 1
  • 8
  • 20
  • 1
    Question is not about xml vs annotation. It's about usage of explicitly defined Beans vs ones which are auto-detected and auto-created by Spring. – vtor Mar 14 '15 at 13:34
0

From my experience, explicit beans are mostly used for third party classes (e.g. to create DataSource, SessionFactory, ...).

If the class under your control and you don't need to have singleton or request scope beans, there is no reason not to component scan such class. It would be needed to explicitly define such bean only if you would need to have weird number of such beans in you app (e.g. 2, 3). In this case you would want probably use named beans. But this situation should be very rare and may point to design problems.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
0

Auto scanning has couple of downsides that we ran into with auto scanning.

  1. If you have java module which is used by few other modules, auto scanning in the java module leads to beans creation whether you need it or not. On the contrary, if auto scanning is not enabled, you can just instantiate the ones that you need via xml in your dependent project. In our application, one of our web module was dependent on 12 different projects and we ran into the similar issue.

  2. Apart from perf aspect, if there are two beans with same id and type like data source bean, in two different java projects and are auto scanned, then there will be a conflict in your dependent project. Xml can help to avoid them if they are not autoscanned.

minion
  • 4,313
  • 2
  • 20
  • 35
  • you said "On the contrary, if auto scanning is not enabled, you can just instantiate the ones that you need via xml in your dependent project." Say i have module 1 which is dependent on module 2. module 1 and module 2 beans are defined in spring config file and loaded at web server start up (by contextLoaderListener). So here also beans will be initialized whether they are needed or nor. So i am nor clear with this point ? – M Sach Mar 15 '15 at 02:42
  • Here is the scenario i am explaining.. Module 1 and module 2 are dependent on module 3. Module 3 package, lets say com.sample has 10 components defined. Lets say all 10 components (will be used in future components etc) are not required by module 1, doing component scan will initialize all 10 components, whereas if you configure through xml, you can defined just the 5 components that you want. This is very common in enterprise app with multiple war modules dependent on common modules, as everyone wants to create a reusable component and trying to put them in common module. – minion Mar 15 '15 at 13:33