1.Aware系列接口
spring 6.0提供了一系列的Aware接口,方便我们在Bean加载时获取信息
如
@Service
public class study implements BeanNameAware {
// spring实例化bean之后调用此方法
@Override
public void setBeanName(String name) {
System.out.println(name);
}
}
结果
study
2.Async异步任务
- spring入口必须开启异步,添加@EnableAsync注解
- 必须被spring托管的bean
- 在方法上添加@Async注解
@EnableAsync
@SpringBootApplication
public class SpringSecurityTestApplication {
public static void main(String[] args) {
ApplicationContext run = SpringApplication.run(SpringSecurityTestApplication.class, args);
Study bean = run.getBean(Study.class);
System.out.println("main方法开始...");
bean.asyncText();
System.out.println("main方法结束...");
}
}
@Component()
public class Study {
@Async
public void asyncText() {
System.out.println("异步执行开始");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("异步执行结束");
}
}
结果
main方法开始...
main方法结束...
异步执行开始
异步执行结束
标签:异步,spring,System,特性,6.0,main,public,out
From: https://www.cnblogs.com/heirem/p/17146620.html