private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) { if (args == null) { return false; } String condition = getCondition(); if (StringUtils.hasText(condition)) { Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null"); return this.evaluator.condition( condition, event, this.targetMethod, this.methodKey, args, this.applicationContext); } return true; }
/** * Specify if the condition defined by the specified expression matches. */ public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod, AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) { EventExpressionRootObject root = new EventExpressionRootObject(event, args); MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext( root, targetMethod, args, getParameterNameDiscoverer()); if (beanFactory != null) { evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue( evaluationContext, Boolean.class))); }
@Test public void beanInstanceRetrievedAtEveryInvocation() { Method method = ReflectionUtils.findMethod( SampleEvents.class, "handleGenericString", GenericTestEvent.class); when(this.context.getBean("testBean")).thenReturn(this.sampleEvents); ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter( "testBean", GenericTestEvent.class, method); listener.init(this.context, new EventExpressionEvaluator()); GenericTestEvent<String> event = createGenericTestEvent("test"); listener.onApplicationEvent(event); verify(this.sampleEvents, times(1)).handleGenericString(event); verify(this.context, times(1)).getBean("testBean"); listener.onApplicationEvent(event); verify(this.sampleEvents, times(2)).handleGenericString(event); verify(this.context, times(2)).getBean("testBean"); }
private List<EventListenerFactory> eventListenerFactories; private final EventExpressionEvaluator evaluator = new EventExpressionEvaluator();
/** * Specify if the condition defined by the specified expression matches. */ public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod, AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) { EventExpressionRootObject root = new EventExpressionRootObject(event, args); MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext( root, targetMethod, args, getParameterNameDiscoverer()); if (beanFactory != null) { evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue( evaluationContext, Boolean.class))); }
private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) { if (args == null) { return false; } String condition = getCondition(); if (StringUtils.hasText(condition)) { Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null"); return this.evaluator.condition( condition, event, this.targetMethod, this.methodKey, args, this.applicationContext); } return true; }
/** * Specify if the condition defined by the specified expression matches. */ public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod, AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) { EventExpressionRootObject root = new EventExpressionRootObject(event, args); MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext( root, targetMethod, args, getParameterNameDiscoverer()); if (beanFactory != null) { evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); } return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue( evaluationContext, Boolean.class))); }
private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) { if (args == null) { return false; } String condition = getCondition(); if (StringUtils.hasText(condition)) { Assert.notNull(this.evaluator, "EventExpressionEvaluator must not be null"); return this.evaluator.condition( condition, event, this.targetMethod, this.methodKey, args, this.applicationContext); } return true; }