Spring 4

Spring 4 @Conditional Annotation

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>As part of my series on the new features in Spring Framework&comma; version 4&comma; I’d like to present how to use the new <i><b>Condition <&sol;b><&sol;i>interface and <b>&commat;Conditional <&sol;b>annotation&period;<&sol;p>&NewLine;<h2><b>&commat;Conditional annotation<&sol;b>&&num;8211&semi;<&sol;h2>&NewLine;<p>Spring 4 is introducing a new feature called Conditional &&num;8211&semi; So it&&num;8217&semi;s time to implement Spring 4 Conditional checking by using &commat;Conditional annotation&period; This annotation with programmatic condition implementation&period; It can react to rich context&period; &commat;Profile annotation support now simply a ProfileCondition implementation class of conditional annotation&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;02&sol;Conditinal-Annotation-in-Spring-4&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b><i>&commat;ImageSource-Slideshare&period;net<&sol;i><&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>There are times when you would like the creation and dependency injection of a bean to depend on circumstances&period; Those circumstance could be what operating system your application is running in&comma; or what application server it finds itself on&period; Bean configuration may also depend on what version of Java is available&comma; the value of a system property&sol;environmental variable&comma; or whether your application is running in dev&comma; test&comma; stage&comma; or production&period; For these and a host of other reasons&comma; you may want to conditionalize the instantiation and wiring of your Spring beans&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Retention&lpar;value&equals;RUNTIME&rpar; &NewLine;&commat;Target&lpar;value&equals;&lbrace;TYPE&comma;METHOD&rcub;&rpar; &NewLine;public &commat;interface Conditional &NewLine;<&sol;pre>&NewLine;<p>Indicates that a component is is only eligible for registration when all specified conditions match&period;<br &sol;>&NewLine;A condition is any state that can be determined pro-grammatically immediately before the bean is due to be created<&sol;p>&NewLine;<p><b>The &commat;Conditional annotation may be used in any of the following ways&colon;<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>as a<b> type-level<&sol;b> annotation on any class directly or indirectly annotated with <b><i>&commat;Component<&sol;i><&sol;b>&comma; including <b><i>&commat;Configuration<&sol;i><&sol;b> classes&period;<&sol;li>&NewLine;<li>as a <b>meta-annotation<&sol;b>&comma; for the purpose of composing custom stereotype annotations&period;<&sol;li>&NewLine;<li>as a <b>method-level<&sol;b> annotation on any <i><b>&commat;Bean <&sol;b><&sol;i>method<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>If a &commat;Configuration class is marked with &commat;Conditional&comma; all of the &commat;Bean methods and &commat;Import annotations associated with that class will be subject to the conditions&period;<&sol;b><&sol;p>&NewLine;<p>The condition interface is as follows&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">public interface Condition&lbrace; &NewLine;&sol;&ast;&ast; Determine if the condition matches&period; &NewLine;&ast; &commat;param context the condition context &NewLine;&ast; &commat;param metadata meta-data of the &lbrace;&commat;link AnnotationMetadata class&rcub; or &NewLine;&ast; &lbrace;&commat;link Method method&rcub; being checked&period; &NewLine;&ast; &commat;return &lbrace;&commat;code true&rcub; if the condition matches and the component can be registered &NewLine;&ast; or &lbrace;&commat;code false&rcub; to veto registration&period; &NewLine;&ast;&sol; &NewLine;boolean matches&lpar;ConditionContext context&comma; AnnotatedTypeMedata metadata&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>SPRING 4&period;0 CONDITIONAL BEAN CONFIGURATION-<&sol;b><&sol;h3>&NewLine;<p>Spring 4 adds a new &commat;Conditional annotation that allows for a similar conditionalized configuration&comma; but one that does not require a profile&period; Using the operating system example again&comma; you would need to create two classes that implement the Spring Condition interface&period; This interface requires the implementation of a matches&lpar; &rpar; method&period; The matches&lpar; &rpar; method checks for a condition and returns a boolean indicating whether that condition is met&period;<&sol;p>&NewLine;<pre class&equals;"highlight">import org&period;springframework&period;context&period;annotation&period;Condition&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;ConditionContext&semi; &NewLine;import org&period;springframework&period;core&period;type&period;AnnotatedTypeMetadata&semi; &NewLine; &NewLine;public class LinuxCondition implements Condition&lbrace; &NewLine; &NewLine; &commat;Override &NewLine; public boolean matches&lpar;ConditionContext context&comma; AnnotatedTypeMetadata metadata&rpar; &lbrace; &NewLine; return context&period;getEnvironment&lpar;&rpar;&period;getProperty&lpar;"os&period;name"&rpar;&period;contains&lpar;"Linux"&rpar;&semi; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<pre class&equals;"highlight">import org&period;springframework&period;context&period;annotation&period;Condition&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;ConditionContext&semi; &NewLine;import org&period;springframework&period;core&period;type&period;AnnotatedTypeMetadata&semi; &NewLine; &NewLine;public class WindowsCondition implements Condition&lbrace; &NewLine; &NewLine; &commat;Override &NewLine; public boolean matches&lpar;ConditionContext context&comma; AnnotatedTypeMetadata metadata&rpar; &lbrace; &NewLine; return context&period;getEnvironment&lpar;&rpar;&period;getProperty&lpar;"os&period;name"&rpar;&period;contains&lpar;"Windows"&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>The <i><b>ConditionContext <&sol;b><&sol;i>parameter to matches&lpar; &rpar; provides access to the environment&comma; container&comma; class loader&comma; etc&period; that the condition may use to make its boolean-producing determination&period; The <b><i>AnnotatedTypeMetadata<&sol;i><&sol;b> parameter to matches&lpar; &rpar; provides access to the method on which the <i><b>&commat;Conditional<&sol;b><&sol;i> using the Condition is applied to&period;<&sol;p>&NewLine;<p>With the conditions in place&comma; now annotate your Configuration bean methods with <i><b>&commat;Conditional <&sol;b><&sol;i>and the condition checking class as a parameter&period; Note how both methods in the Configuration return an <b><i>EmailService<&sol;i><&sol;b> implementation named &OpenCurlyDoubleQuote;<b><i>emailerService<&sol;i><&sol;b>”&period; However&comma; only one of these methods will get called by the container and create the <i><b>emailerService <&sol;b><&sol;i>based on the conditions provided through &commat;Conditional&period;<&sol;p>&NewLine;<pre class&equals;"highlight">import org&period;springframework&period;context&period;annotation&period;Bean&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;Conditional&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;Configuration&semi; &NewLine; &NewLine;&commat;Configuration &NewLine;public class MyConfiguration &lbrace; &NewLine; &NewLine; &commat;Bean&lpar;name&equals;"emailerService"&rpar; &NewLine; &commat;Conditional&lpar;WindowsCondition&period;class&rpar; &NewLine; public EmailService windowsEmailerService&lpar;&rpar;&lbrace; &NewLine; return new WindowsEmailService&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;Bean&lpar;name&equals;"emailerService"&rpar; &NewLine; &commat;Conditional&lpar;LinuxCondition&period;class&rpar; &NewLine; public EmailService linuxEmailerService&lpar;&rpar;&lbrace; &NewLine; return new LinuxEmailService&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>The example here uses the conditions for the creation of just one bean&period; However&comma; the conditions serve as a more reusable mechanism&comma; which could be used to conditionalize all sorts of beans&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Spring MVC Related Posts<&sol;b><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Web Tutorial<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interview Questions<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-mvc&sol;"><span style&equals;"color&colon; red&semi;">MVC Design Pattern<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;what-is-dispatcherservlet-in-spring-and-its-uses&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC DispatcherServlet <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC WebApplicationContext and Root Application Context<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;stereotype-annotations-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;Controller Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestmapping-annotation-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestMapping Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-annotation-in-spring-mvc-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestParam Annotation<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-applicationcontext-webapplicationcontext-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC ContextLoaderListener<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;requestparam-vs-pathvariable-annotations-in-spring-mvc&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC &commat;RequestParam and &commat;PathVariable annotations<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-30-mvc-hello-world-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Hello World Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-exception-handling-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Exception Handling Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-mvc-with-hibernate-crud-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC with Hibernate CRUD Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-tiles-plugin-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Tiles Plugin with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-and-interceptor-with&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Interceptor with example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;integration-spring-mvc3-and-mongodb&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC with MongoDB CRUD Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-3-mvc-internationalization&sol;"><span style&equals;"color&colon; red&semi;">Spring MVC Internationalization &amp&semi; Localization with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;restcontroller-in-spring-40-framework&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-4-tutorials-step-to-new-spring&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;controlleradvice-improvements-in-spring&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;restcontroller-in-spring-40-framework&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;controlleradvice-improvements-in-spring&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '232'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago