有沒有什麼辦法可以在Spring應用程序中獲得已經創建的特定Prototype-bean的實體的編號和一些標識信息??在Spring應用程序中已經創建了特定Prototype-bean的實體的數量?
加法。在我們的項目中,我們有400多個原型bean,我想跟蹤執行期間創建的bean的狀態以及每種類型的實體數量。
有沒有什麼辦法可以在Spring應用程序中獲得已經創建的特定Prototype-bean的實體的編號和一些標識信息??在Spring應用程序中已經創建了特定Prototype-bean的實體的數量?
加法。在我們的項目中,我們有400多個原型bean,我想跟蹤執行期間創建的bean的狀態以及每種類型的實體數量。
你可以通過發佈和收聽應用程序事件來完成。
我這裏是 Spring – Publish and Listen Application Events
春不管理的原型bean的整個生命週期:容器初始化,配置,裝飾或者是裝配一個原型對象,將它交給客戶端,然後有對原型實例沒有進一步的瞭解。
簡單的變體:
public class PrototypeCreationEvent extends ApplicationEvent {
private String beanName;
public PrototypeCreationEvent(Object source , String beanName) {
super(source);
this.beanName = beanName;
}
public String getBeanName(){
return beanName;
}
}
public class PrototypeCreationListener implements ApplicationListener<PrototypeCreationEvent> {
private ConcurrentMap<String,AtomicInteger> prototypeCreationStatistic = new ConcurrentHashMap<>();
//or from guava AtomicLongMap prototypeCreationStatistic = AtomicLongMap.create();
@Override
public void onApplicationEvent(PrototypeCreationEvent event) {
prototypeCreationStatistic.computeIfAbsent(event.getBeanName() , k->new AtomicInteger(0)).incrementAndGet();
System.out.println(event);
}
public ConcurrentMap<String,AtomicInteger> getPrototypeCreationStatistic(){
return prototypeCreationStatistic;
}
}
public abstract class PrototypeCreationPublisher implements BeanNameAware , ApplicationEventPublisherAware ,InitializingBean {
private String beanName;
private ApplicationEventPublisher applicationEventPublisher;
@Override
public void setBeanName(String name) {
this.beanName = name;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println();
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@PostConstruct //or use interface InitializingBean
public void sendEventAfterCreation() throws Exception {
applicationEventPublisher.publishEvent(new PrototypeCreationEvent(this , beanName));
}
}
@Component(value = BeanDefinition.SCOPE_PROTOTYPE)
public class PrototypeA extends PrototypeCreationPublisher{
}
@Component(value = BeanDefinition.SCOPE_PROTOTYPE)
public class PrototypeB extends PrototypeCreationPublisher{
}
例如:
PrototypeA prototypeA1 = context.getBean(PrototypeA.class);
PrototypeA prototypeA2 = context.getBean(PrototypeA.class);
PrototypeA prototypeA3 = context.getBean(PrototypeA.class);
PrototypeB prototypeB1 = context.getBean(PrototypeB.class);
PrototypeCreationListener statistic = context.getBean(PrototypeCreationListener.class);
statistic.getPrototypeCreationStatistic().entrySet().forEach(s->{
System.out.println(s.getKey() + " count = "+s.getValue());
});
結果:
PrototypeB count = 1
PrototypeA count = 3
謝謝。有什麼辦法從ApplicationContext獲取信息嗎? –
好的。我會盡力。 –
可能是我不明白的東西......但我可以通過僅從bean的構造函數打印控制檯中的信息來獲取有關創建bean的信息。請看我更正的問題。在我們的項目中,我們有400多個原型bean,所以我想檢查一下原型bean的創建和數量。 –