Skip to content

Autowiring with @Autowired/@Inject get much slower than with @Resource as the number of classes increases [SPR-13086] #17677

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

  • Pattern1 @Inject
  • Pattern2 @Inject + @Named
  • Pattern3 @Autowired
  • Pattern4 @Autowired + @Qualifier
  • Pattern5 @Resource

I generated a lot of components with the script .
This script generates N Controller s, Service s, and ServiceImpl s.
The result was as follows:

N 1000 2000 3000 4000 5000 6000
Pattern 1 8549 21171 35018 55879 86589 112140
Pattern 2 9547 19830 35662 57174 85846 108736
Pattern 3 9282 19121 35586 57377 81274 106385
Pattern 4 9126 19394 36292 57704 81762 110087
Pattern 5 6663 11203 16427 23868 30603 33978
(unit=milli second)

Graph.png

At that time, I used Spring 4.1.6.RELEASE with the following environment:

  • OS: Windows 7 64bit
  • CPU: Core i7-4610M CPU @ 3.00GHz
  • RAM: 16GB
  • JDK: Oracle JDK 8u45

Previous versions will be same.

@Inject and @Autowired (Pattern 1-4) seems to take O(N^2) time.
On the other hand @Resource seems to take O(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

  • Autowiring by type gets faster
  • @Autowired + @Qualifier and @Inject + @Named became as fast as @Resource

Affects: 4.1.6

Attachments:

Issue Links:

8 votes, 12 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions