Spring Bean 초기화 중에 포인트 컷 / 내부 (point cut / within)를 넣어 비즈니스에 필요한 통계를 계산할 수있는 Spring AOP 기능을 개발하고 싶습니다. 스프링 AOP 모듈을 사용할 수 있는지 알고 싶습니다.
이 구성 요소를 사용하여 초기화 시간을 측정 할 수 있습니다.
@Component
public class MyBeanPostProcessor implements BeanPostProcessor, Ordered {
private Map<String, Long> start;
private Map<String, Long> end;
public MyBeanPostProcessor() {
start = new HashMap<>();
end = new HashMap<>();
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
start.put(beanName, System.currentTimeMillis());
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
end.put(beanName, System.currentTimeMillis());
return bean;
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
//this method returns initialization time of the bean.
public long initializationTime(String beanName) {
return end.get(beanName) - start.get(beanName);
}
}
그러나 이번에는 생성자를 실행하는 시간을 포함하지 않습니다.
그러나 모든 빈 생성자가 실행되기 전에 빈 정의를 읽은 후 잠시 기록 할 수 있습니다. BeanFactoryPostProccessor를 사용하십시오.
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
private long launchTime;
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
launchTime = System.currentTimeMillis();
}
public long getLaunchTime() {
return launchTime;
}
}
lauchTime은 스프링이 구성 (예 : xml 파일) 읽기를 마쳤고 빈을 만들 준비가 된 순간입니다.
따라서 전체 초기화 시간은 max (end)-launchTime과 같은 두 가지 구성 요소를 사용하여 계산할 수 있습니다. (마지막 빈 초기화 시간과 빈 설정을 읽은 시간의 차이)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다