Skip to content

Commit 4efb964

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 5c798e3 + 8fe9dd9 commit 4efb964

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

spring-test/src/main/java/org/springframework/wx/AnnoAC.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.expression.spel.standard.SpelExpressionParser;
66
import org.springframework.expression.spel.support.StandardEvaluationContext;
77
import org.springframework.wx.beans4test.circularReference.A;
8+
import org.springframework.wx.beans4test.lazy.NotALazyBean;
89
import org.springframework.wx.beans4test.methodOveride.Student;
910
import 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
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

0 commit comments

Comments
 (0)