Rajdeep
Rajdeep

Reputation: 270

How to Load only selective classes or beans in Spring

I have an Interface say A, and five implementations for it say A1, A2, A3, A4, A5 in the same package. Now when starting spring application, I only want to load one say A1 out of A1, A2, A3, A4, A5 depending upon autowiring. I dont want to load others as it will make the application heavy if there are many classes like those. Please explain possible answer.

Upvotes: 0

Views: 1901

Answers (1)

Rudziankoŭ
Rudziankoŭ

Reputation: 11251

@Qualifier

Assume you have following context:

<bean id="a1_beanId" class="com.A1" >
</bean>

<bean id="a2_beanId" class="com.A2" >
</bean>

You have to use qualifier to autowire A interface with correct realization. Autowiring then happens by bean id.

@Autowired
@Qualifier("a1_beanId")
private A yourA1Bean;

@Lazy

To prevent beans being loaded to spring context you have to switch on lazy mode using annotation @Lazy or lazy-init="true" for xml config.

A lazily-initialized bean indicates to the IoC container whether or not a bean instance should be created at startup or when it is first requested.

Upvotes: 2

Related Questions