Skip to content

Commit 37059b5

Browse files
committed
0501作业,写代码实现 Spring Bean 的装配
1、在applicationContext.xml中使用xml方式配置 bean = faculty2040 2、在applicationContext.xml中使用xml方式配置 bean = faculty2080,使用@resource注解注入到Klass实例(目前是class1)的属性teacher2080中 3、使用@bean说明create()返回的是一个bean,并(在SpringDemo01的main()中)调用create() HelloBeanDefinitionRegistryPostProcessor中也可以调用不含@bean注解的Create()方法,但需要: 3.1、调用registry.registerBeanDefinition()加入到context中 3.2、调用beanFactory.registerSingleton()加入到context中
1 parent 3d76b10 commit 37059b5

9 files changed

Lines changed: 239 additions & 20 deletions

File tree

03concurrency/0301/src/main/java/java0/conc0301/op/Join.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void main(String[] args) {
1515
if (i == 20) {
1616
try {
1717
oo.wait(0);
18-
//thread1.join();
18+
// thread1.join();
1919
} catch (InterruptedException e) {
2020
e.printStackTrace();
2121
}
@@ -46,6 +46,8 @@ public void run() {
4646
for (int i = 0; i < 100; i++) {
4747
System.out.println(name + i);
4848
}
49+
// with synchronized(oo) and oo.wait(), run as wish using the next line.
50+
oo.notifyAll();
4951
}
5052
}
5153

03concurrency/0301/src/main/java/java0/conc0301/op/WaitAndNotify.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
public class WaitAndNotify {
44
public static void main(String[] args) {
55
MethodClass methodClass = new MethodClass();
6+
MethodKlass methodKlass = new MethodKlass();
67
Thread t1 = new Thread(() -> {
78
try {
89
methodClass.product();
@@ -13,7 +14,7 @@ public static void main(String[] args) {
1314
}, "t1");
1415
Thread t2 = new Thread(() -> {
1516
try {
16-
methodClass.customer();
17+
methodKlass.customer();
1718
} catch (InterruptedException e) {
1819
// TODO Auto-generated catch block
1920
e.printStackTrace();
@@ -46,7 +47,7 @@ public synchronized void product() throws InterruptedException {
4647
Thread.sleep(10);
4748
if (productCount >= MAX_COUNT) {
4849
System.out.println("货舱已满,,.不必再生产");
49-
50+
5051
wait();
5152
}else {
5253
productCount++;
@@ -70,4 +71,7 @@ public synchronized void customer() throws InterruptedException {
7071
notifyAll();
7172
}
7273
}
74+
}
75+
class MethodKlass extends MethodClass {
76+
7377
}
Lines changed: 129 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package java0.conc0303;
22

3-
import java.util.concurrent.CountDownLatch;
3+
import java.util.concurrent.*;
4+
import java.lang.reflect.*;
45

56
/**
67
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池,
@@ -10,21 +11,111 @@
1011
* 一个简单的代码参考:
1112
*/
1213
public class Homework03 {
13-
14+
// static CountDownLatch countDownLatch = new CountDownLatch(1);
15+
1416
public static void main(String[] args) {
15-
16-
long start=System.currentTimeMillis();
1717

18-
// 在这里创建一个线程或线程池,
18+
long start = System.currentTimeMillis();
19+
20+
// 在这里创建一个线程或线程池
21+
// ExecutorService service = Executors.newSingleThreadExecutor();
22+
ExecutorService service = Executors.newCachedThreadPool();
1923
// 异步执行 下面方法
20-
21-
int result = sum(); //这是得到的返回值
22-
23-
// 确保 拿到result 并输出
24-
System.out.println("异步计算结果为:"+result);
25-
24+
// 1. FutureTask
25+
FutureTask<Integer> futureTask = new FutureTask(() -> sum());
26+
service.submit(futureTask);
27+
try {
28+
int result = futureTask.get().intValue();
29+
30+
// int result = sum(); //这是得到的返回值
31+
32+
// 确保 拿到result 并输出
33+
System.out.println("1 : FutureTask异步计算结果为:" + result);
34+
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms");
35+
} catch (InterruptedException e) {
36+
e.printStackTrace();
37+
} catch (ExecutionException e) {
38+
e.printStackTrace();
39+
}
40+
41+
// 2. Callable + Future
42+
Future future = service.submit(() -> sum());
43+
try {
44+
int result = (Integer) future.get();
45+
46+
// 确保 拿到result 并输出
47+
System.out.println("2 : Future + Callable异步计算结果为:" + result);
48+
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms");
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
} catch (ExecutionException e) {
52+
e.printStackTrace();
53+
} finally {
54+
// 关闭cached线程池, 以便结束JVM进程, 否则需要60秒空闲才能没有线程
55+
service.shutdown();
56+
}
57+
58+
// 3. Thread + Future + Callable
59+
ComputeThread task3 = new ComputeThread();
60+
FutureTask<Integer> futureTask3 = new FutureTask<>(task3);
61+
new Thread(futureTask3).start();
62+
try {
63+
int result = (Integer)futureTask3.get();
64+
65+
// 确保 拿到result 并输出
66+
System.out.println("3 : Thread + FutureTask + Callable异步计算结果为:"+result);
67+
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms");
68+
} catch (InterruptedException e) {
69+
e.printStackTrace();
70+
} catch (ExecutionException e) {
71+
e.printStackTrace();
72+
}
73+
74+
// 4. Thread + FutureTask + Callable + join()
75+
ComputeThread task4 = new ComputeThread();
76+
FutureTask<Integer> futureTask4 = new FutureTask<>(task4);
77+
Thread thread = new Thread(futureTask4);
78+
try {
79+
thread.start();
80+
thread.join();
81+
int result = (Integer)futureTask4.get();
82+
83+
// 确保 拿到result 并输出
84+
System.out.println("4 : Thread + FutureTask + Callable + join()异步计算结果为:"+result);
85+
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms");
86+
} catch (InterruptedException e) {
87+
e.printStackTrace();
88+
} catch (ExecutionException e) {
89+
e.printStackTrace();
90+
}
91+
92+
// 5. CompletableFuture.get() + Callable
93+
// CountDownLatch countDownLatch = new CountDownLatch(1);
94+
CompletableFuture<Integer> cfSum = CompletableFuture.supplyAsync(()-> {
95+
int result = sum();
96+
// countDownLatch.countDown();
97+
return result;
98+
});
99+
try {
100+
int result = cfSum.get();
101+
cfSum.complete(100);
102+
System.out.println("5 : CompletableFuture.get() + Callable异步计算结果为:"+result);
103+
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms");
104+
// countDownLatch.await();
105+
} catch (InterruptedException e) {
106+
e.printStackTrace();
107+
} catch (ExecutionException e) {
108+
e.printStackTrace();
109+
}
110+
111+
// 6. CompletableFuture.join() + Callable
112+
CompletableFuture<Integer> cfSum6 = CompletableFuture.supplyAsync(()-> {
113+
int result = sum();
114+
return result;
115+
});
116+
int result = cfSum6.join();
117+
System.out.println("6 : CompletableFuture.join() + Callable异步计算结果为:"+result);
26118
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms");
27-
28119
// 然后退出main线程
29120
}
30121

@@ -37,4 +128,30 @@ private static int fibo(int a) {
37128
return 1;
38129
return fibo(a-1) + fibo(a-2);
39130
}
131+
132+
}
133+
134+
class ComputeThread implements Callable<Integer> {
135+
@Override
136+
public Integer call() throws Exception {
137+
Method method1 = Homework03.class.getDeclaredMethod("sum");
138+
method1.setAccessible(true);
139+
Integer res = (Integer)method1.invoke(null);
140+
return res.intValue();
141+
}
40142
}
143+
144+
class ComputeTask implements Callable<Integer> {
145+
private CountDownLatch countDownLatch;
146+
public ComputeTask(CountDownLatch countDownLatch) {
147+
this.countDownLatch = countDownLatch;
148+
}
149+
@Override
150+
public Integer call() throws Exception {
151+
Method method1 = Homework03.class.getDeclaredMethod("sum");
152+
method1.setAccessible(true);
153+
Integer res = (Integer)method1.invoke(null);
154+
countDownLatch.countDown();
155+
return res.intValue();
156+
}
157+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.kimmking.spring01;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import lombok.ToString;
7+
import org.springframework.beans.factory.BeanNameAware;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.context.ApplicationContextAware;
10+
import org.springframework.context.annotation.Bean;
11+
12+
import java.io.Serializable;
13+
14+
@Data
15+
@AllArgsConstructor
16+
@NoArgsConstructor
17+
@ToString
18+
public class Faculty implements Serializable, BeanNameAware, ApplicationContextAware {
19+
private int id;
20+
private String name;
21+
22+
private String beanName;
23+
private ApplicationContext applicationContext;
24+
25+
public void init(){
26+
System.out.println("hello..., this is a faculty");
27+
}
28+
29+
// 3、使用@Bean说明create()返回的是一个bean,并(在SpringDemo01的main()中)调用create()
30+
// HelloBeanDefinitionRegistryPostProcessor中也可以调用不含@Bean注解的Create()方法,但需要:
31+
// 3.1、调用registry.registerBeanDefinition()加入到context中
32+
// 3.2、调用beanFactory.registerSingleton()加入到context中
33+
@Bean
34+
public static Faculty create(){
35+
return new Faculty(1020,"Faculty1020","Faculty === POJO", null);
36+
}
37+
38+
public void print() {
39+
System.out.println(this.beanName);
40+
System.out.println(" context.getBeanDefinitionNames() ===>> "
41+
+ String.join(",", applicationContext.getBeanDefinitionNames()));
42+
43+
}
44+
}

04fx/spring01/src/main/java/io/kimmking/spring01/Student.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Student implements Serializable, BeanNameAware, ApplicationContextA
2727
private ApplicationContext applicationContext;
2828

2929
public void init(){
30-
System.out.println("hello...........");
30+
System.out.println("hello...Student........");
3131
}
3232

3333
public static Student create(){

04fx/spring01/src/main/java/io/kimmking/spring02/Klass.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package io.kimmking.spring02;
22

3+
import io.kimmking.spring01.Faculty;
34
import io.kimmking.spring01.Student;
45
import lombok.Data;
56

7+
import javax.annotation.Resource;
68
import java.util.List;
79

810
@Data
911
public class Klass {
1012

1113
List<Student> students;
12-
14+
15+
// 1、xml方式配置 bean = faculty2040,并装入bean = class1 的属性teacher中
16+
Faculty teacher;
17+
18+
// 2、xml方式配置 bean = faculty2080,使用@Resource注解注入到Klass实例(目前是class1)的属性teacher2080中
19+
@Resource(name = "faculty2080")
20+
Faculty teacher2080;
21+
22+
1323
public void dong(){
1424
System.out.println(this.getStudents());
1525
}

04fx/spring01/src/main/java/io/kimmking/spring02/School.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.kimmking.spring02;
22

33
import io.kimmking.aop.ISchool;
4+
import io.kimmking.spring01.Faculty;
45
import io.kimmking.spring01.Student;
56
import lombok.Data;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,11 +17,17 @@ public class School implements ISchool {
1617

1718
@Resource(name = "student100")
1819
Student student100;
19-
20+
21+
@Resource(name = "student123")
22+
Student student123;
23+
24+
@Resource(name = "faculty2040")
25+
Faculty faculty2040;
26+
2027
@Override
2128
public void ding(){
2229

23-
System.out.println("Class1 have " + this.class1.getStudents().size() + " students and one is " + this.student100);
30+
System.out.println("Class1 have " + this.class1.getStudents().size() + " students and one is " + this.student100 + ".");
2431

2532
}
2633

04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.kimmking.spring02;
22

33
import io.kimmking.aop.ISchool;
4+
import io.kimmking.spring01.Faculty;
45
import io.kimmking.spring01.Student;
56
import org.springframework.context.ApplicationContext;
67
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -10,7 +11,8 @@ public class SpringDemo01 {
1011
public static void main(String[] args) {
1112

1213
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
13-
// Student student123 = context.getBean(Student.class);
14+
Student student101 = (Student)context.getBean("s101");
15+
System.out.println(student101.toString());
1416

1517
Student student123 = (Student) context.getBean("student123");
1618
System.out.println(student123.toString());
@@ -49,5 +51,19 @@ public static void main(String[] args) {
4951
if (s102 != null) {
5052
System.out.println(s102);
5153
}
54+
// Student s108 = (Student) context.getBean("s108");
55+
// if (s102 != null) {
56+
// System.out.println(s108);
57+
// }
58+
59+
60+
// 3、使用@Bean说明create()返回的是一个bean,并(在SpringDemo01的main()中)调用create()
61+
// HelloBeanDefinitionRegistryPostProcessor中也可以调用不含@Bean注解的Create()方法,但需要:
62+
// 3.1、调用registry.registerBeanDefinition()加入到context中
63+
// 3.2、调用beanFactory.registerSingleton()加入到context中
64+
Faculty f1020 = Faculty.create();
65+
if (f1020 != null) {
66+
System.out.println(f1020);
67+
}
5268
}
5369
}

04fx/spring01/src/main/resources/applicationContext.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
<property name="id" value="100" />
2020
<property name="name" value="KK100" />
2121
</bean>
22+
23+
<!-- 1、xml方式配置 bean = faculty2040-->
24+
<bean id="faculty2040"
25+
class="io.kimmking.spring01.Faculty">
26+
<property name="id" value="2040" />
27+
<property name="name" value="FF2040" />
28+
</bean>
29+
30+
<!-- 2、xml方式配置 bean = faculty2080,使用@Resource注解注入到Klass实例(目前是class1)的属性teacher2080中-->
31+
<bean id="faculty2080"
32+
class="io.kimmking.spring01.Faculty">
33+
<property name="id" value="2080" />
34+
<property name="name" value="FF2080" />
35+
</bean>
2236

2337
<bean id="class1" class="io.kimmking.spring02.Klass">
2438
<property name="students">
@@ -31,6 +45,11 @@
3145
<!-- </bean>-->
3246
</list>
3347
</property>
48+
49+
<!-- 1、xml方式配置 bean = faculty2040,并装入bean = class1 的属性teacher中-->
50+
<property name="teacher">
51+
<ref bean="faculty2040" />
52+
</property>
3453
</bean>
3554

3655

0 commit comments

Comments
 (0)