15
15
*/
16
16
package org .springframework .data .repository .init ;
17
17
18
+ import javax .annotation .Nonnull ;
19
+
18
20
import org .springframework .beans .factory .FactoryBean ;
19
21
import org .springframework .beans .factory .config .AbstractFactoryBean ;
20
22
import org .springframework .context .ApplicationContext ;
@@ -37,7 +39,7 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
37
39
extends AbstractFactoryBean <ResourceReaderRepositoryPopulator >
38
40
implements ApplicationListener <ContextRefreshedEvent >, ApplicationContextAware {
39
41
40
- private @ Nullable Resource [] resources ;
42
+ private Resource [] resources = new Resource [ 0 ] ;
41
43
private @ Nullable RepositoryPopulator populator ;
42
44
private @ Nullable ApplicationContext context ;
43
45
@@ -64,6 +66,7 @@ public void setApplicationContext(ApplicationContext applicationContext) {
64
66
* (non-Javadoc)
65
67
* @see org.springframework.beans.factory.config.AbstractFactoryBean#getObjectType()
66
68
*/
69
+ @ Nonnull
67
70
@ Override
68
71
public Class <?> getObjectType () {
69
72
return ResourceReaderRepositoryPopulator .class ;
@@ -78,7 +81,10 @@ protected ResourceReaderRepositoryPopulator createInstance() {
78
81
79
82
ResourceReaderRepositoryPopulator initializer = new ResourceReaderRepositoryPopulator (getResourceReader ());
80
83
initializer .setResources (resources );
81
- initializer .setApplicationEventPublisher (context );
84
+
85
+ if (context != null ) {
86
+ initializer .setApplicationEventPublisher (context );
87
+ }
82
88
83
89
this .populator = initializer ;
84
90
@@ -91,14 +97,25 @@ protected ResourceReaderRepositoryPopulator createInstance() {
91
97
*/
92
98
public void onApplicationEvent (ContextRefreshedEvent event ) {
93
99
100
+ RepositoryPopulator populator = this .populator ;
101
+
102
+ if (populator == null ) {
103
+ throw new IllegalStateException ("RepositoryPopulator was not properly initialized!" );
104
+ }
105
+
94
106
if (event .getApplicationContext ().equals (context )) {
107
+
95
108
Repositories repositories = new Repositories (event .getApplicationContext ());
96
109
populator .populate (repositories );
97
110
}
98
111
}
99
112
100
113
protected abstract ResourceReader getResourceReader ();
101
114
115
+ /*
116
+ * (non-Javadoc)
117
+ * @see org.springframework.beans.factory.config.AbstractFactoryBean#afterPropertiesSet()
118
+ */
102
119
@ Override
103
120
public void afterPropertiesSet () throws Exception {
104
121
0 commit comments