20
20
21
21
import javax .persistence .Entity ;
22
22
import javax .persistence .EntityManager ;
23
+ import javax .persistence .Query ;
24
+ import javax .persistence .metamodel .EntityType ;
23
25
import java .time .Duration ;
26
+ import java .util .ArrayList ;
27
+ import java .util .List ;
24
28
import java .util .Set ;
25
29
26
30
/**
27
31
* Created on May, 2018
28
32
*
29
33
* @author destan
30
34
*/
35
+ @ Slf4j
31
36
@ Configuration
32
37
@ AutoConfigureBefore (MessageSourceAutoConfiguration .class )
33
- @ Slf4j
34
38
class DatabaseResourceBundleAutoconfiguration {
35
39
36
40
@ Bean
@@ -42,7 +46,7 @@ MessageSourceProperties messageSourceProperties() {
42
46
@ Bean
43
47
@ ConditionalOnMissingBean (BundleContentLoaderStrategy .class )
44
48
BundleContentLoaderStrategy bundleContentLoaderStrategy (EntityManager entityManager ) throws ClassNotFoundException {
45
- return new JpaBundleContentLoaderStrategy <>(entityManager , findBundleEntityConcreteClass ());
49
+ return new JpaBundleContentLoaderStrategy <>(entityManager , findBundleEntityConcreteClass (entityManager ));
46
50
}
47
51
48
52
@ Bean
@@ -80,39 +84,24 @@ private MessageSource createMessageSource(MessageSourceProperties properties, Bu
80
84
}
81
85
82
86
@ SuppressWarnings ("unchecked" )
83
- private Class <? extends BundleEntity > findBundleEntityConcreteClass () throws ClassNotFoundException {
84
- final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (false );
85
-
86
- scanner .addIncludeFilter (new AnnotationTypeFilter (Entity .class ));
87
- scanner .addIncludeFilter (new AssignableTypeFilter (BundleEntity .class ));
87
+ private Class <? extends BundleEntity > findBundleEntityConcreteClass (EntityManager entityManager ) throws ClassNotFoundException {
88
88
89
- final StopWatch stopWatch = new StopWatch ();
90
- stopWatch .start ("findCandidateComponents with 'com'" );
89
+ final List <Class <?>> entities = new ArrayList <>();
91
90
92
- Set <BeanDefinition > entities = scanner .findCandidateComponents (
93
- "com" );//first try this because giving a base significantly improves performance
94
- if (entities .isEmpty ()) {
95
- stopWatch .stop ();
96
- stopWatch .start ("findCandidateComponents with '*'" );
97
- entities = scanner .findCandidateComponents ("" );
91
+ for (EntityType <?> entity : entityManager .getMetamodel ().getEntities ()) {
92
+ if (BundleEntity .class .isAssignableFrom (entity .getJavaType ())) {
93
+ entities .add (entity .getJavaType ());
94
+ }
98
95
}
99
- stopWatch .stop ();
100
96
101
- log .debug ("Scanned in {}" , stopWatch .prettyPrint ());
97
+ if (entities .size () == 1 ) {
98
+ return (Class <? extends BundleEntity >) entities .get (0 );
99
+ }
102
100
103
101
if (entities .size () > 1 ) {
104
102
throw new IllegalStateException ("There must be exactly one implementation of BundleEntity annotated with @Entity." );
105
103
}
106
104
107
- for (BeanDefinition bd : entities ) {
108
- try {
109
- return (Class <? extends BundleEntity >) Class .forName (bd .getBeanClassName ());
110
- }
111
- catch (ClassNotFoundException e ) {
112
- log .error (e .getMessage (), e );
113
- }
114
- }
115
-
116
105
throw new ClassNotFoundException ("There should be one class annotated with @Entity and implements BundleEntity." );
117
106
}
118
107
0 commit comments