夜光序言:
我比任何人都希望你幸福,只是想到将来你的幸福不是因为我还是会很难过。
正文:
以道御术 / 以术识道
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
package 并发编程.案例1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration //配置
@ComponentScan("并发编程.案例1") //夜光:扫描的包
@EnableAsync //支持异步同步
public class Config {
}
package 并发编程.案例1;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
//然后,我们来创建一个运行的类
public class Main {
public static void main(String[] args) {
//嗯~第一步,我们先创建一个AnnotationConfigApplicationContext
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
//其次
//获取bean
DemoService dc = ac.getBean(DemoService.class);
//之后,夜光:执行一下方法
dc.a();
dc.b();
}
}
package 并发编程.案例1;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration //配置
@ComponentScan("并发编程.案例1") //夜光:扫描的包
@EnableAsync //支持异步同步
public class Config {
}
package 并发编程.案例1;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service //交给spring容器来进行管理
public class DemoService {
@Async //夜光:异步的注解
public void a(){
while (true){
System.out.println("a");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Async //夜光:异步的注解
public void b(){
while (true){
System.out.println("b");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}