Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out what exactly does Spring autowire?

I have a large Spring 2.x-based application with a couple of hundreds of applicationContext.xml files and several thousands beans/bean factories.

Most of these XML configurations say something like default-autowire="byName", effectively turning on the autowiring, but only a fraction of beans is actually autowired. Most of the bean properties are set explicitly.

(This is to historical reasons, I guess that's how you call it when you were not clever enough in the past.)

Now we would like to remove autowiring at all. We believe that only a small fraction of beans is actually autowired - but we do not know, what and were exactly. My question is:

How can we find out what and were exactly is autowired by Spring?

Ideally, we need to get a list of beans/properties so that we could inject these explicitly in out XML configurations. But before diving into Spring internals with a debugger, I decided to ask if someone on SF has maybe already solved a similar task.

ps. I don't have an intention to discuss whether autowiring is good or bad. We have a number of internal technical reasons to remove autowiring, that's all.

like image 578
lexicore Avatar asked Sep 21 '11 07:09

lexicore


People also ask

Does Spring @autowired inject beans by name or by type?

Spring @Autowired Annotation - Service ClassThe setter method will be used for spring autowiring byName and byType whereas constructor based injection will be used by constructor autowire attribute.

How the Auto Detect Autowiring internally works?

Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. First it will try to look for valid constructor with arguments, If found the constructor mode is chosen. If there is no constructor defined in bean, or explicit default no-args constructor is present, the autowire byType mode is chosen.

How Autowire identifies the bean type?

Autowiring 'byType': This option enables the autowire based on bean type. It searches the property's class type in the configuration file. It injects the property if such bean is found; otherwise, an error is raised.

What is the default Autowire in Spring?

The default autowire mode in java configuration is byType .


2 Answers

You can try to enable DEBUG logging in Spring. It prints a lot of information during initialisation phase. I bet auto-wiring messages are also printed out. You just need to find that message and then parse the log file after application is fully initialised.

UPDATE: I believe AbstractAutowireCapableBeanFactory is responsible for auto-wiring logic. You can check autowireByName method. It produces the following log message which you can search for in the log file:

logger.debug("Added autowiring by name from bean name '" + beanName +
    "' via property '" + propertyName + "' to bean named '" + propertyName + "'"); 
like image 76
Andrey Adamovich Avatar answered Oct 13 '22 23:10

Andrey Adamovich


My guess would be to enable a logger (log4j) for that package (the one which does autowire) in a specific file.

That would output the name of all of the beans.

like image 41
ssedano Avatar answered Oct 14 '22 00:10

ssedano