2

I am a bit puzzled about Spring bean-scanning.

AFAIK I need to put <context:component-scan base-package="some.package" />

and Spring will search from the root of a given package for beans.

But now in my application, if I delete this line from .xml configuration file, and start up Tomcat I get

INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4dd1b: defining beans

and then I get a long list of beans, some of them are from Spring, but also there are those that would I would expect to see with <context:component-scan base-package="some.package" /> added to config file. The only thing that is different between both logs, is the mapping of the controller methods to addresses. Methods are not mapped if this line is not present.

Why Spring is detecting those beans? What is making Spring scan my packages for annotated classes?

If it is relevant I am using Spring 3

EDIT: if I don't remove <context:component-scan base-package="some.package" /> I am getting information about pre-instantiating singletons twice. So it looks like those beans are scanned two times.

EDIT: It seems that in one of the files loaded by ContextListener I had component-scan. That is why I had those beans listed twice.

Andna
  • 6,539
  • 13
  • 71
  • 120

3 Answers3

0

You must have <context:component-scan base-package="se.eleon" />in your config.xml thats where spring is scaning for all your code if you doesn't put anything in there nothing will happen cuz spring mvc is expecting it. This is where spring is looking for all annotation etc

MByD
  • 135,866
  • 28
  • 264
  • 277
  • The thing is, it seems that Spring detects my beans even without `context:component-scan`, that is the problem, I am wondering why? – Andna Aug 07 '12 at 20:18
0

Do you also have <context:annotation-config/> in your application context XML? If so, it would explain why the beans are initialized without <context:component-scan .../>.

This SO post actually explains the difference between the two.

Community
  • 1
  • 1
mindas
  • 26,463
  • 15
  • 97
  • 154
  • I searched using Eclipse all files in my project and this line is only encountered in servlet-context.xml. To be precise, we have four .xml files:root-context.xml, servlet-context.xml, security-context.xml and data-context.xml – Andna Aug 07 '12 at 21:29
  • I'm guessing this is enough to get your beans scanned. Did you try to remove it and see what happens? – mindas Aug 07 '12 at 21:35
  • After all it seems that I had twice written context:component-scan, sorry for all the trouble. – Andna Aug 07 '12 at 22:23
0

Do you have two dispatcher xml configurations files which are scanning same package ? If that so beans initialization happens twice.

The reason to have this automatic scanning process is the identify you annotations like @Controller @Autowire etc. Otherwise those annotation do not make any sense to Spring IOC container.

Eshan Sudharaka
  • 607
  • 2
  • 9
  • 16