File tree Expand file tree Collapse file tree 4 files changed +113
-2
lines changed
spring-test/src/main/java/org/springframework/wx Expand file tree Collapse file tree 4 files changed +113
-2
lines changed Original file line number Diff line number Diff line change 55import org .springframework .expression .spel .standard .SpelExpressionParser ;
66import org .springframework .expression .spel .support .StandardEvaluationContext ;
77import org .springframework .wx .beans4test .circularReference .A ;
8+ import org .springframework .wx .beans4test .lazy .NotALazyBean ;
89import org .springframework .wx .beans4test .methodOveride .Student ;
910import org .springframework .wx .beans4test .methodOveride .Teacher ;
1011
@@ -67,8 +68,11 @@ public static void main(String[] args) {
6768// System.out.println(value);
6869
6970 // 测试importSelector
70- AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext ("org.springframework.wx.importSelector" );
71- annotationConfigApplicationContext .getBean ("personL" );
71+ // AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("org.springframework.wx.importSelector");
72+ // annotationConfigApplicationContext.getBean("personL");
73+ // 实现懒加载bean
74+ AnnotationConfigApplicationContext context4LazyTest = new AnnotationConfigApplicationContext ("org.springframework.wx.beans4test.lazy" );
75+ ((NotALazyBean )context4LazyTest .getBean ("notALazyBean" )).getIamALazyBean ().getaBeanNeedAName ();
7276
7377
7478 }
Original file line number Diff line number Diff line change 1+ package org .springframework .wx .beans4test .lazy ;
2+
3+ import org .springframework .stereotype .Component ;
4+
5+ /**
6+ * @author wuxin
7+ * @date 2023/03/03 16:19:49
8+ */
9+ @ Component
10+ public class IamALazyBean {
11+
12+
13+ private String aBeanNeedAName ;
14+
15+
16+ public IamALazyBean () {
17+ System .out .println ("懒加载bean的初始化" );
18+ }
19+
20+ public String getaBeanNeedAName () {
21+ return aBeanNeedAName ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package org .springframework .wx .beans4test .lazy ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .context .annotation .Lazy ;
5+ import org .springframework .stereotype .Component ;
6+
7+ /**
8+ * @author wuxin
9+ * @date 2023/03/03 16:21:39
10+ */
11+ @ Component
12+ public class NotALazyBean {
13+
14+ @ Autowired
15+ @ Lazy
16+ private IamALazyBean iamALazyBean ;
17+
18+ public NotALazyBean () {
19+ System .out .println ("非懒bean的初始化" );
20+ }
21+
22+
23+ public IamALazyBean getIamALazyBean () {
24+ return iamALazyBean ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package org .springframework .wx .lifesycle ;
2+
3+ import org .springframework .context .SmartLifecycle ;
4+ import org .springframework .stereotype .Component ;
5+
6+ /**
7+ * @author wuxin
8+ * @date 2023/04/18 16:28:21
9+ */
10+ @ Component
11+ public class MySmartLifeCycle implements SmartLifecycle {
12+
13+
14+ private boolean isRuning = false ;
15+
16+ /**
17+ * isRunning是false的时候才会执行
18+ */
19+ @ Override
20+ public void start () {
21+ System .out .println ("容器现在开始启动" );
22+ isRuning = true ;
23+ }
24+
25+ /**
26+ * jvm默认会等所有的hook都执行完毕之后才会 关闭虚拟机
27+ * isRunning是true的时候才会执行
28+ */
29+ @ Override
30+ public void stop () {
31+ System .out .println ("现在关闭开始----------------------------------现在关闭" );
32+
33+ for (int i = 1 ; i < 100 ; i ++) {
34+ try {
35+ Thread .sleep (1000 );
36+ System .out .println (("执行" + i + "次" ));
37+ } catch (InterruptedException e ) {
38+ throw new RuntimeException (e );
39+ }
40+ }
41+
42+ System .out .println ("现在关闭结束----------------------------------现在关闭" );
43+ isRuning = false ;
44+ }
45+
46+ /**
47+ * 在DefualtLifeCycleProcessor在调用这些是实现类的时候
48+ * 会先检测容器是否还存货,如果不在存活则不会调用
49+ * 也就是说这里给了false这里就不会被调用
50+ * @return
51+ */
52+ @ Override
53+ public boolean isRunning () {
54+ return isRuning ;
55+ }
56+
57+
58+ }
You can’t perform that action at this time.
0 commit comments