Spring Boot容器加载完后操作
@Component
public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
private Logger logger = LoggerFactory.getLogger(ApplicationStartup.class);
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Map<String,Object> map = event.getApplicationContext().getBeansWithAnnotation(Configuration.class);
if (map != null) {
for(Object bean : map.values()){
logger.info("bean:{}",bean.getClass().getName());
}
}
User user = event.getApplicationContext().getBean(User.class);
logger.info("user:{}",user.getClass().getName());
User test = new User();
test.setName("test");
event.getApplicationContext().publishEvent(test);
}
}
@Service
class EventTest{
@EventListener
public void placeOrderNotice(User user) {
System.out.println("name:"+user.getName());
}
}
@Configuration
class User{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}