3 回答
TA贡献1966条经验 获得超4个赞
从Spring 4.2开始,您可以使用@EventListener(文档)
@Component
class MyClassWithEventListeners {
@EventListener({ContextRefreshedEvent.class})
void contextRefreshedEvent() {
System.out.println("a context refreshed event happened");
}
}
TA贡献1784条经验 获得超7个赞
创建您的注释
@Retention(RetentionPolicy.RUNTIME)
public @interface AfterSpringLoadComplete {
}
建立课程
public class PostProxyInvokerContextListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
ConfigurableListableBeanFactory factory;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext context = event.getApplicationContext();
String[] names = context.getBeanDefinitionNames();
for (String name : names) {
try {
BeanDefinition definition = factory.getBeanDefinition(name);
String originalClassName = definition.getBeanClassName();
Class<?> originalClass = Class.forName(originalClassName);
Method[] methods = originalClass.getMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(AfterSpringLoadComplete.class)){
Object bean = context.getBean(name);
Method currentMethod = bean.getClass().getMethod(method.getName(), method.getParameterTypes());
currentMethod.invoke(bean);
}
}
} catch (Exception ignored) {
}
}
}
}
通过@Component注释或在xml中注册此类
<bean class="ua.adeptius.PostProxyInvokerContextListener"/>
并在环境初始化后要在任何要使用的方法上使用注解,例如:
@AfterSpringLoadComplete
public void init() {}
- 3 回答
- 0 关注
- 526 浏览
添加回答
举报