Spring - applicationContext getBeansWithAnnotation method returns an empty list
My question is about getBeansWithAnnotation method.
I have a custom annotation named MyCustomAnnotation.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Scope("prototype")
public @interface MyCustomAnnotation {
String group() default "DEFAULT_GROUP";
}
I also have a listener class like below:
public class MyCustomAnnotationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
Map<String, Object> myCustomAnnotationBeans = applicationContext.getBeansWithAnnotation(MyCustomAnnotation.class);
}
}
I have configured my application-context.xml to scan components with MyCustomAnnotation.
<context:include-filter type="annotation" expression="com.annotations.MyCustomAnnotation"/>
I can get the list of beans which are annotated with MyCustomAnnotation during initial startup of my application using getBeansWithAnnotation method in MyCustomAnnotationListener.
My question is why this method returns an empty list when it is triggered the second time.
Thanks
java spring spring-annotations
|
show 3 more comments
My question is about getBeansWithAnnotation method.
I have a custom annotation named MyCustomAnnotation.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Scope("prototype")
public @interface MyCustomAnnotation {
String group() default "DEFAULT_GROUP";
}
I also have a listener class like below:
public class MyCustomAnnotationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
Map<String, Object> myCustomAnnotationBeans = applicationContext.getBeansWithAnnotation(MyCustomAnnotation.class);
}
}
I have configured my application-context.xml to scan components with MyCustomAnnotation.
<context:include-filter type="annotation" expression="com.annotations.MyCustomAnnotation"/>
I can get the list of beans which are annotated with MyCustomAnnotation during initial startup of my application using getBeansWithAnnotation method in MyCustomAnnotationListener.
My question is why this method returns an empty list when it is triggered the second time.
Thanks
java spring spring-annotations
when will you invoke 2nd times? actually , as your@MyCustomAnnotationis also marked with@Component, it will be picked up by Spring by default , no need to set theinclude-filter
– Ken Chan
Dec 29 '18 at 11:20
I do not invoke refresh event manually; it is invoked right after the first time.
– AliceInWonderland
Dec 29 '18 at 11:42
You meanMyCustomAnnotationListener#onApplicationEventinvoke 2 times ?
– Ken Chan
Dec 29 '18 at 11:49
Yes you are right; it invokes two times. During the first one I got the beans with annotations; and during the second call I got an empty map.
– AliceInWonderland
Dec 29 '18 at 11:51
They are the same context ? (Check byapplicationContext.getId()) . Your context has a parent context?
– Ken Chan
Dec 29 '18 at 11:59
|
show 3 more comments
My question is about getBeansWithAnnotation method.
I have a custom annotation named MyCustomAnnotation.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Scope("prototype")
public @interface MyCustomAnnotation {
String group() default "DEFAULT_GROUP";
}
I also have a listener class like below:
public class MyCustomAnnotationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
Map<String, Object> myCustomAnnotationBeans = applicationContext.getBeansWithAnnotation(MyCustomAnnotation.class);
}
}
I have configured my application-context.xml to scan components with MyCustomAnnotation.
<context:include-filter type="annotation" expression="com.annotations.MyCustomAnnotation"/>
I can get the list of beans which are annotated with MyCustomAnnotation during initial startup of my application using getBeansWithAnnotation method in MyCustomAnnotationListener.
My question is why this method returns an empty list when it is triggered the second time.
Thanks
java spring spring-annotations
My question is about getBeansWithAnnotation method.
I have a custom annotation named MyCustomAnnotation.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Scope("prototype")
public @interface MyCustomAnnotation {
String group() default "DEFAULT_GROUP";
}
I also have a listener class like below:
public class MyCustomAnnotationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
Map<String, Object> myCustomAnnotationBeans = applicationContext.getBeansWithAnnotation(MyCustomAnnotation.class);
}
}
I have configured my application-context.xml to scan components with MyCustomAnnotation.
<context:include-filter type="annotation" expression="com.annotations.MyCustomAnnotation"/>
I can get the list of beans which are annotated with MyCustomAnnotation during initial startup of my application using getBeansWithAnnotation method in MyCustomAnnotationListener.
My question is why this method returns an empty list when it is triggered the second time.
Thanks
java spring spring-annotations
java spring spring-annotations
asked Dec 29 '18 at 10:12
AliceInWonderlandAliceInWonderland
83
83
when will you invoke 2nd times? actually , as your@MyCustomAnnotationis also marked with@Component, it will be picked up by Spring by default , no need to set theinclude-filter
– Ken Chan
Dec 29 '18 at 11:20
I do not invoke refresh event manually; it is invoked right after the first time.
– AliceInWonderland
Dec 29 '18 at 11:42
You meanMyCustomAnnotationListener#onApplicationEventinvoke 2 times ?
– Ken Chan
Dec 29 '18 at 11:49
Yes you are right; it invokes two times. During the first one I got the beans with annotations; and during the second call I got an empty map.
– AliceInWonderland
Dec 29 '18 at 11:51
They are the same context ? (Check byapplicationContext.getId()) . Your context has a parent context?
– Ken Chan
Dec 29 '18 at 11:59
|
show 3 more comments
when will you invoke 2nd times? actually , as your@MyCustomAnnotationis also marked with@Component, it will be picked up by Spring by default , no need to set theinclude-filter
– Ken Chan
Dec 29 '18 at 11:20
I do not invoke refresh event manually; it is invoked right after the first time.
– AliceInWonderland
Dec 29 '18 at 11:42
You meanMyCustomAnnotationListener#onApplicationEventinvoke 2 times ?
– Ken Chan
Dec 29 '18 at 11:49
Yes you are right; it invokes two times. During the first one I got the beans with annotations; and during the second call I got an empty map.
– AliceInWonderland
Dec 29 '18 at 11:51
They are the same context ? (Check byapplicationContext.getId()) . Your context has a parent context?
– Ken Chan
Dec 29 '18 at 11:59
when will you invoke 2nd times? actually , as your
@MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter– Ken Chan
Dec 29 '18 at 11:20
when will you invoke 2nd times? actually , as your
@MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter– Ken Chan
Dec 29 '18 at 11:20
I do not invoke refresh event manually; it is invoked right after the first time.
– AliceInWonderland
Dec 29 '18 at 11:42
I do not invoke refresh event manually; it is invoked right after the first time.
– AliceInWonderland
Dec 29 '18 at 11:42
You mean
MyCustomAnnotationListener#onApplicationEvent invoke 2 times ?– Ken Chan
Dec 29 '18 at 11:49
You mean
MyCustomAnnotationListener#onApplicationEvent invoke 2 times ?– Ken Chan
Dec 29 '18 at 11:49
Yes you are right; it invokes two times. During the first one I got the beans with annotations; and during the second call I got an empty map.
– AliceInWonderland
Dec 29 '18 at 11:51
Yes you are right; it invokes two times. During the first one I got the beans with annotations; and during the second call I got an empty map.
– AliceInWonderland
Dec 29 '18 at 11:51
They are the same context ? (Check by
applicationContext.getId()) . Your context has a parent context?– Ken Chan
Dec 29 '18 at 11:59
They are the same context ? (Check by
applicationContext.getId()) . Your context has a parent context?– Ken Chan
Dec 29 '18 at 11:59
|
show 3 more comments
1 Answer
1
active
oldest
votes
ContextRefreshedEvent should happen once during bootstrapping the context. It will publish the event to the context itself and all of its parent contexts.
Now its listener (i.e MyCustomAnnotationListener) executes 2 times suggesting that your context may have a parent context .The @MyCustomAnnotation beans are defined in child context , and so the parent context cannot find it and empty list is return when MyCustomAnnotationListener runs for the parent context.
You may verify if the context are the same using applicationContext.getId().
BTW : As @MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968592%2fspring-applicationcontext-getbeanswithannotation-method-returns-an-empty-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
ContextRefreshedEvent should happen once during bootstrapping the context. It will publish the event to the context itself and all of its parent contexts.
Now its listener (i.e MyCustomAnnotationListener) executes 2 times suggesting that your context may have a parent context .The @MyCustomAnnotation beans are defined in child context , and so the parent context cannot find it and empty list is return when MyCustomAnnotationListener runs for the parent context.
You may verify if the context are the same using applicationContext.getId().
BTW : As @MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter.
add a comment |
ContextRefreshedEvent should happen once during bootstrapping the context. It will publish the event to the context itself and all of its parent contexts.
Now its listener (i.e MyCustomAnnotationListener) executes 2 times suggesting that your context may have a parent context .The @MyCustomAnnotation beans are defined in child context , and so the parent context cannot find it and empty list is return when MyCustomAnnotationListener runs for the parent context.
You may verify if the context are the same using applicationContext.getId().
BTW : As @MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter.
add a comment |
ContextRefreshedEvent should happen once during bootstrapping the context. It will publish the event to the context itself and all of its parent contexts.
Now its listener (i.e MyCustomAnnotationListener) executes 2 times suggesting that your context may have a parent context .The @MyCustomAnnotation beans are defined in child context , and so the parent context cannot find it and empty list is return when MyCustomAnnotationListener runs for the parent context.
You may verify if the context are the same using applicationContext.getId().
BTW : As @MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter.
ContextRefreshedEvent should happen once during bootstrapping the context. It will publish the event to the context itself and all of its parent contexts.
Now its listener (i.e MyCustomAnnotationListener) executes 2 times suggesting that your context may have a parent context .The @MyCustomAnnotation beans are defined in child context , and so the parent context cannot find it and empty list is return when MyCustomAnnotationListener runs for the parent context.
You may verify if the context are the same using applicationContext.getId().
BTW : As @MyCustomAnnotation is also marked with @Component , it will be picked up by Spring by default , no need to set the include-filter.
answered Dec 29 '18 at 13:42
Ken ChanKen Chan
39k1594112
39k1594112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968592%2fspring-applicationcontext-getbeanswithannotation-method-returns-an-empty-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
when will you invoke 2nd times? actually , as your
@MyCustomAnnotationis also marked with@Component, it will be picked up by Spring by default , no need to set theinclude-filter– Ken Chan
Dec 29 '18 at 11:20
I do not invoke refresh event manually; it is invoked right after the first time.
– AliceInWonderland
Dec 29 '18 at 11:42
You mean
MyCustomAnnotationListener#onApplicationEventinvoke 2 times ?– Ken Chan
Dec 29 '18 at 11:49
Yes you are right; it invokes two times. During the first one I got the beans with annotations; and during the second call I got an empty map.
– AliceInWonderland
Dec 29 '18 at 11:51
They are the same context ? (Check by
applicationContext.getId()) . Your context has a parent context?– Ken Chan
Dec 29 '18 at 11:59