Revise test for ApplicationListener self injection
See gh-28322
This commit is contained in:
@@ -41,7 +41,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.PayloadApplicationEvent;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.event.test.self_inject.MyApplication;
|
||||
import org.springframework.context.event.test.self_inject.MyAspect;
|
||||
import org.springframework.context.event.test.self_inject.MyEventListener;
|
||||
import org.springframework.context.event.test.self_inject.MyEventPublisher;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
@@ -280,17 +280,17 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
|
||||
* Regression test for <a href="https://github.com/spring-projects/spring-framework/issues/28283">issue 28283</a>,
|
||||
* where event listeners proxied due to e.g.
|
||||
* <ul>
|
||||
* <li>{@code @Transactional} annotations in their methods or</li>
|
||||
* <li>being targeted by aspects</li>
|
||||
* <li>{@code @Transactional} annotations in their methods or</li>
|
||||
* <li>being targeted by aspects</li>
|
||||
* </ul>
|
||||
* were added to the list of application listener beans twice (both proxy and unwrapped target).
|
||||
*/
|
||||
@Test
|
||||
public void eventForSelfInjectedProxiedListenerFiredOnlyOnce() {
|
||||
String basePackage = MyApplication.class.getPackageName();
|
||||
AbstractApplicationContext context = new AnnotationConfigApplicationContext(basePackage);
|
||||
AbstractApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
MyAspect.class, MyEventListener.class, MyEventPublisher.class);
|
||||
context.getBean(MyEventPublisher.class).publishMyEvent("hello");
|
||||
assertThat(MyEventListener.eventCount).isEqualTo(1);
|
||||
assertThat(context.getBean(MyEventListener.class).eventCount).isEqualTo(1);
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.springframework.context.event.test.self_inject;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
|
||||
@Configuration
|
||||
@EnableAspectJAutoProxy(proxyTargetClass = true)
|
||||
public class MyApplication {
|
||||
public static void main(String[] args) {
|
||||
try (AbstractApplicationContext context = new AnnotationConfigApplicationContext("org.springframework.context.event.test.self_inject")) {
|
||||
context.getBean(MyEventPublisher.class).publishMyEvent("hello");
|
||||
assert MyEventListener.eventCount == 1 : "event listener must fire exactly once";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,34 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.event.test.self_inject;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class MyAspect {
|
||||
|
||||
@Before("within(org.springframework.context.event.test.self_inject.MyEventListener)")
|
||||
public void myAdvice(JoinPoint joinPoint) {
|
||||
//System.out.println(joinPoint);
|
||||
// System.out.println(joinPoint);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,31 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.event.test.self_inject;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class MyEvent extends ApplicationEvent {
|
||||
|
||||
private String message;
|
||||
|
||||
public MyEvent(Object source, String message) {
|
||||
super(source);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.event.test.self_inject;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -6,15 +22,15 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyEventListener implements ApplicationListener<MyEvent> {
|
||||
public static int eventCount;
|
||||
|
||||
@Autowired // use '-Dspring.main.allow-circular-references=true' in Spring Boot >= 2.6.0
|
||||
//@Lazy // with '@Lazy', the problem does not occur
|
||||
public int eventCount;
|
||||
|
||||
@Autowired
|
||||
private MyEventListener eventDemoListener;
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(MyEvent event) {
|
||||
//System.out.println("Event: " + event);
|
||||
eventCount++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.event.test.self_inject;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -6,10 +22,12 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyEventPublisher {
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
|
||||
public void publishMyEvent(String message) {
|
||||
eventPublisher.publishEvent(new MyEvent(this, message));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user