File tree Expand file tree Collapse file tree 4 files changed +31
-2
lines changed
org/springframework/beans/factory Expand file tree Collapse file tree 4 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -8,5 +8,6 @@ public static void main(String[] args) {
8
8
BeanFactory beanFactory = new BeanFactory ();
9
9
beanFactory .instantiate ("main.java.si.inspirited" );
10
10
beanFactory .populateProperties ();
11
+ beanFactory .injectBeanNames ();
11
12
}
12
13
}
Original file line number Diff line number Diff line change 1
1
package main .java .si .inspirited ;
2
2
3
+ import org .springframework .beans .factory .BeanNameAware ;
3
4
import org .springframework .beans .factory .stereotype .Component ;
4
5
5
6
@ Component
6
- public class PromotionsService {
7
+ public class PromotionsService implements BeanNameAware {
7
8
9
+ String beanName ;
10
+
11
+ @ Override
12
+ public void setBeanName (String name ) {
13
+ this .beanName = name ;
14
+ }
15
+
16
+ public String getBeanName () {
17
+ return this .beanName ;
18
+ }
8
19
}
Original file line number Diff line number Diff line change 4
4
import org .springframework .beans .factory .stereotype .Component ;
5
5
6
6
import java .io .File ;
7
+ import java .io .IOException ;
7
8
import java .lang .reflect .Field ;
8
9
import java .lang .reflect .InvocationTargetException ;
9
10
import java .lang .reflect .Method ;
11
+ import java .net .URISyntaxException ;
10
12
import java .net .URL ;
11
13
import java .util .Enumeration ;
12
14
import java .util .HashMap ;
@@ -46,7 +48,7 @@ public void instantiate(String basePackage) {
46
48
}
47
49
}
48
50
}
49
- }catch (Exception e ) {e .printStackTrace ();}
51
+ }catch (NoSuchMethodException | ClassNotFoundException | IOException | InstantiationException | IllegalAccessException | InvocationTargetException | URISyntaxException e ) {e .printStackTrace ();}
50
52
System .out .println (singletons );
51
53
}
52
54
@@ -70,4 +72,13 @@ public void populateProperties(){
70
72
}
71
73
}
72
74
}
75
+
76
+ public void injectBeanNames (){
77
+ for (String name : singletons .keySet ()) {
78
+ Object bean = singletons .get (name );
79
+ if (bean instanceof BeanNameAware ){
80
+ ((BeanNameAware ) bean ).setBeanName (name );
81
+ }
82
+ }
83
+ }
73
84
}
Original file line number Diff line number Diff line change
1
+ package org .springframework .beans .factory ;
2
+
3
+ public interface BeanNameAware {
4
+
5
+ void setBeanName (String name );
6
+ }
You can’t perform that action at this time.
0 commit comments