优化ApplicationContextAware

master
xuchengsheng 2023-10-18 20:41:55 +08:00
parent c6556d1f8d
commit 320a6b784f
1 changed files with 1 additions and 3 deletions

View File

@ -96,15 +96,13 @@ public interface ApplicationContextAware extends Aware {
### 五、最佳实践 ### 五、最佳实践
首先来看看启动类入口,上下文环境使用`AnnotationConfigApplicationContext`此类是使用Java注解来配置Spring容器的方式构造参数我们给定了一个`MyConfiguration`组件类。然后从Spring上下文中获取一个`MyApplicationContextAware`类型的bean最后调用`publish`方法用于发布一个事件。 首先来看看启动类入口,上下文环境使用`AnnotationConfigApplicationContext`此类是使用Java注解来配置Spring容器的方式构造参数我们给定了一个`MyConfiguration`组件类。
```java ```java
public class ApplicationContextAwareApplication { public class ApplicationContextAwareApplication {
public static void main(String[] args) { public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
MyApplicationContextAware contextAware = context.getBean(MyApplicationContextAware.class);
contextAware.publish("hello world");
} }
} }
``` ```