Order 3rd party spring filters at bean creation time
I know filters can be annotated with @Order() but if the filters are all included from different 3rd party libraries can I order them when I create the bean?
@Bean(Ordered.HIGHEST_PRECEDENCE) // Illegal!!!, just an example
SomeFilter someFilter() {
// this runs before someOtherFilter
return new SomeFilter();
}
@Bean(Ordered.LOWEST_PRECEDENCE) // Illegal!!!, just an example
SomeOtherFilter someOtherFilter() {
// this runs after someFilter
return new SomeOtherFilter();
}
java spring spring-boot
add a comment |
I know filters can be annotated with @Order() but if the filters are all included from different 3rd party libraries can I order them when I create the bean?
@Bean(Ordered.HIGHEST_PRECEDENCE) // Illegal!!!, just an example
SomeFilter someFilter() {
// this runs before someOtherFilter
return new SomeFilter();
}
@Bean(Ordered.LOWEST_PRECEDENCE) // Illegal!!!, just an example
SomeOtherFilter someOtherFilter() {
// this runs after someFilter
return new SomeOtherFilter();
}
java spring spring-boot
The code above - is it a putative part of a configuration class?
– Andremoniy
Nov 12 '18 at 11:09
It's an example but sure, it must be in a configration class. It's an illegal example though, @bean doesn't take an int as default parameter and no ordering at all.
– Andreas Wederbrand
Nov 12 '18 at 11:20
add a comment |
I know filters can be annotated with @Order() but if the filters are all included from different 3rd party libraries can I order them when I create the bean?
@Bean(Ordered.HIGHEST_PRECEDENCE) // Illegal!!!, just an example
SomeFilter someFilter() {
// this runs before someOtherFilter
return new SomeFilter();
}
@Bean(Ordered.LOWEST_PRECEDENCE) // Illegal!!!, just an example
SomeOtherFilter someOtherFilter() {
// this runs after someFilter
return new SomeOtherFilter();
}
java spring spring-boot
I know filters can be annotated with @Order() but if the filters are all included from different 3rd party libraries can I order them when I create the bean?
@Bean(Ordered.HIGHEST_PRECEDENCE) // Illegal!!!, just an example
SomeFilter someFilter() {
// this runs before someOtherFilter
return new SomeFilter();
}
@Bean(Ordered.LOWEST_PRECEDENCE) // Illegal!!!, just an example
SomeOtherFilter someOtherFilter() {
// this runs after someFilter
return new SomeOtherFilter();
}
java spring spring-boot
java spring spring-boot
edited Nov 12 '18 at 11:21
Andreas Wederbrand
asked Nov 12 '18 at 11:05
Andreas WederbrandAndreas Wederbrand
26.7k64865
26.7k64865
The code above - is it a putative part of a configuration class?
– Andremoniy
Nov 12 '18 at 11:09
It's an example but sure, it must be in a configration class. It's an illegal example though, @bean doesn't take an int as default parameter and no ordering at all.
– Andreas Wederbrand
Nov 12 '18 at 11:20
add a comment |
The code above - is it a putative part of a configuration class?
– Andremoniy
Nov 12 '18 at 11:09
It's an example but sure, it must be in a configration class. It's an illegal example though, @bean doesn't take an int as default parameter and no ordering at all.
– Andreas Wederbrand
Nov 12 '18 at 11:20
The code above - is it a putative part of a configuration class?
– Andremoniy
Nov 12 '18 at 11:09
The code above - is it a putative part of a configuration class?
– Andremoniy
Nov 12 '18 at 11:09
It's an example but sure, it must be in a configration class. It's an illegal example though, @bean doesn't take an int as default parameter and no ordering at all.
– Andreas Wederbrand
Nov 12 '18 at 11:20
It's an example but sure, it must be in a configration class. It's an illegal example though, @bean doesn't take an int as default parameter and no ordering at all.
– Andreas Wederbrand
Nov 12 '18 at 11:20
add a comment |
1 Answer
1
active
oldest
votes
Since you cannot add the @Order annotation on filters, you can still use FilterRegistrationBean like this:
@Bean
public FilterRegistrationBean someFilter()
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
SomeFilter filter = new SomeFilter();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/bla/*");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
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%2f53260840%2forder-3rd-party-spring-filters-at-bean-creation-time%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
Since you cannot add the @Order annotation on filters, you can still use FilterRegistrationBean like this:
@Bean
public FilterRegistrationBean someFilter()
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
SomeFilter filter = new SomeFilter();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/bla/*");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
add a comment |
Since you cannot add the @Order annotation on filters, you can still use FilterRegistrationBean like this:
@Bean
public FilterRegistrationBean someFilter()
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
SomeFilter filter = new SomeFilter();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/bla/*");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
add a comment |
Since you cannot add the @Order annotation on filters, you can still use FilterRegistrationBean like this:
@Bean
public FilterRegistrationBean someFilter()
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
SomeFilter filter = new SomeFilter();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/bla/*");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
Since you cannot add the @Order annotation on filters, you can still use FilterRegistrationBean like this:
@Bean
public FilterRegistrationBean someFilter()
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
SomeFilter filter = new SomeFilter();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/bla/*");
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
edited Nov 12 '18 at 11:39
answered Nov 12 '18 at 11:27
user10639668
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
add a comment |
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
Does this mean I have to know all the filters that gets declared? I really want to order just a few of them and don't have to care about the rest.
– Andreas Wederbrand
Nov 12 '18 at 11:39
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
@AndreasWederbrand The above example deals only with SomeFilter instance. You can ignore anything else as you please!!
– user10639668
Nov 12 '18 at 11:42
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53260840%2forder-3rd-party-spring-filters-at-bean-creation-time%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
The code above - is it a putative part of a configuration class?
– Andremoniy
Nov 12 '18 at 11:09
It's an example but sure, it must be in a configration class. It's an illegal example though, @bean doesn't take an int as default parameter and no ordering at all.
– Andreas Wederbrand
Nov 12 '18 at 11:20