-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Autowiring with @Autowired/@Inject get much slower than with @Resource as the number of classes increases [SPR-13086] #17677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Toshiaki Maki commented The result of measuring start-up time for each pattern |
Has there been any progress with this? I've run into issues with slow application start up and tests due to this, and I haven't found a way of wiring specifically by name other than:
Another possibility could be defining beans with Is there any other solution for wiring by name using constructor injection and annotations that I'm missing? |
We intend to revisit this within our common dependency injection algorithm, shortcutting constructor parameter resolution when the parameter name matches a bean name as long as the bean type matches as well (and as long as no primary beans overrule such a match). See #28122 for that effort, hopefully addressing this ticket along with it in the 6.2 timeframe. |
It's nice that this is finally tackled, but I hope there are other solutions considered or that the shortcutting is general enough for our usecase. I'm saying this because our bean names usually match the name of the class / interface, e.g. In any case, I want to stress that it's good that you have this among the milestones, and even if the initial solution doesn't work for us, it might open doors for other future improvements with which we might be able to use to tackle our performance issues with beans wiring. |
In addition to the shortcut for parameter name matches in #28122, there is equivalent shortcut behavior for |
Toshiaki Maki opened SPR-13086 and commented
I had a performance issue at the project which had a few thousand of
@Component
classes. The start up time of the application took a few minutes.That application used
@Autowired
for autowiring and the start up time reduced to less than one minute after swiching@Autowired
to@Resource
.So, I measured the time to create
ApplicationContext
for the following patterns:@Inject
@Inject + @Named
@Autowired
@Autowired + @Qualifier
@Resource
I generated a lot of components with the script .
This script generates N
Controller
s,Service
s, andServiceImpl
s.The result was as follows:
At that time, I used Spring 4.1.6.RELEASE with the following environment:
Previous versions will be same.
@Inject
and@Autowired
(Pattern 1-4) seems to takeO(N^2)
time.On the other hand
@Resource
seems to takeO(N)
time.I guess autowirng "by name" is faster than "by type" because the number of candidates for each "type" increase as the target of component-scan increases and
@Qualifier
,@Named
doesn't give any optimization for autowiring.So I hope
@Autowired
+@Qualifier
and@Inject
+@Named
became as fast as@Resource
Affects: 4.1.6
Attachments:
Issue Links:
8 votes, 12 watchers
The text was updated successfully, but these errors were encountered: