Lazy Beans in Spring

eidher - Oct 5 '20 - - Dev Community

Beans are created on startup when the application context is created (eagerly). But, lazy beans are created the first time they are used. Its use is not recommended.

@Lazy
@Component("jdbcRepository")
public class JdbcRepositoryImpl implements AppRepository {
    ...
}
Enter fullscreen mode Exit fullscreen mode

When we put the @Lazy annotation over the @Configuration class, all the methods with @Bean annotation should be loaded lazily.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .