Order 3rd party spring filters at bean creation time












2














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();
}









share|improve this question
























  • 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
















2














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();
}









share|improve this question
























  • 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














2












2








2


1





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();
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












1 Answer
1






active

oldest

votes


















1














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;
}





share|improve this answer























  • 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













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
});


}
});














draft saved

draft discarded


















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









1














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;
}





share|improve this answer























  • 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


















1














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;
}





share|improve this answer























  • 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
















1












1








1






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;
}





share|improve this answer














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;
}






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation