1 /*
2 * Copyright 2018-2020 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.springframework.data.repository.config;
17
18 import lombok.NonNull;
19 import lombok.RequiredArgsConstructor;
20 import lombok.extern.slf4j.Slf4j;
21
22 import org.springframework.beans.factory.ListableBeanFactory;
23 import org.springframework.context.ApplicationListener;
24 import org.springframework.context.event.ContextRefreshedEvent;
25 import org.springframework.core.Ordered;
26 import org.springframework.data.repository.Repository;
27
28 /**
29 * {@link ApplicationListener} to trigger the initialization of Spring Data repositories right before the application
30 * context is started.
31 *
32 * @author Oliver Gierke
33 * @since 2.1
34 * @soundtrack Dave Matthews Band - Here On Out (Come Tomorrow)
35 */
36 @Slf4j
37 @RequiredArgsConstructor
38 class DeferredRepositoryInitializationListener implements ApplicationListener<ContextRefreshedEvent>, Ordered {
39
40 private final @NonNull ListableBeanFactory beanFactory;
41
42 /*
43 * (non-Javadoc)
44 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
45 */
46 @Override
47 public void onApplicationEvent(ContextRefreshedEvent event) {
48
49 LOG.info("Triggering deferred initialization of Spring Data repositories…");
50
51 beanFactory.getBeansOfType(Repository.class);
52
53 LOG.info("Spring Data repositories initialized!");
54 }
55
56 /*
57 * (non-Javadoc)
58 * @see org.springframework.core.Ordered#getOrder()
59 */
60 @Override
61 public int getOrder() {
62 return Ordered.HIGHEST_PRECEDENCE;
63 }
64 }
65