Spring autowire by name constructor argument

We have large java applications with thousands of beans that profile the slow initialization of the spring context, we found that automatic wiring by type is what takes up most of the context loading time. An autowiring bean by name seems to improve context initialization by an order of magnitude. However, I could not find a way to autowire beans by name as constructor arguments.

Below is an authorized member that works fine, spring searches by name:

class MyClass {
   @Resource
   private MyBean mybean;
}

But since @Resource only applies to members and methods, this does not apply to the constructor. We tried using @Inject with @Named and @Autowired with @Qualifier, but spring does a slow search by type:

class MyClass {
   @Inject
   public MyClass(@Named("myBean") MyBean myBean) {
   }
}

class MyClass {

   @Autowired
   public MyClass(@Qualifer("myBean") MyBean myBean) {
   }
}

spring ?

( bean), .

+5
1

, , xml, Spring xml java.

bean myBean xml java,

<bean id="myClass" class="...MyClass">
    <constructor-arg ref = "myBean"/>
</bean>

:

<bean id="myClass" class="...MyClass">
    <constructor-arg index="0" ref = "myBean"/>
    <constructor-arg index="1" ref = "myOtherBean"/>
</bean>

java- :

@Configuration
public class MyClassConfig() {

@Autowire
@Qualifier("myBean")
MyBean myBean;

@Bean
MyClass myclass = new MyClass(myBean);
}

jny .

0

All Articles