{"id":1553,"date":"2015-03-13T23:20:04","date_gmt":"2015-03-13T22:20:04","guid":{"rendered":"http:\/\/codingexplained.com\/?p=1553"},"modified":"2017-06-11T17:38:29","modified_gmt":"2017-06-11T15:38:29","slug":"enabling-spring-bean-for-multiple-profiles","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles","title":{"rendered":"Enabling Spring Bean for Multiple Profiles"},"content":{"rendered":"<p>This is going to be a quick one. I just want to show you how one can\u00a0define a bean in Spring Framework\u00a0for multiple profiles. That is, the bean will be &#8220;active&#8221; if the active profile is one in a given list. This is for example useful if you want different beans to be available at runtime depending on the environment in which the application is deployed. An example of this is a <span class=\"code\">TextEncryptor<\/span>; you might not want to encrypt data on your development machine, because you might want to be able to read the data within the database without having to decrypt it. On the contrary, in this example, you would want to encrypt data in staging and production environments for security purposes. Such a situation can be handled by using the <span class=\"code\">@Profile<\/span> annotation together with the <span class=\"code\">@Bean<\/span> annotation, as in the below example.<\/p>\n<pre><code class=\"java\">@Configuration\r\npublic class SecurityConfig {\r\n    @Profile(&quot;development&quot;)\r\n    @Bean\r\n    public TextEncryptor textEncryptorNoOp() {\r\n        return Encryptors.noOpText();\r\n    }\r\n\r\n    @Profile({ &quot;production&quot;, &quot;staging&quot; })\r\n    @Bean\r\n    public TextEncryptor textEncryptor() {\r\n        return Encryptors.queryableText(&quot;password&quot;, &quot;salt&quot;);\r\n    }\r\n}<\/code><\/pre>\n<p>Notice that the return types of both beans are the same. When we request a bean of type <span class=\"code\">TextEncryptor<\/span> (e.g. by using the <span class=\"code\">@Autowired<\/span> annotation), we will not get an error if we do not use a qualifier, because Spring will know which one to use if an active Spring profile is defined, e.g. via <span class=\"code\">-Dspring.profiles.active=&#8221;development&#8221;<\/span>. Also notice that the <span class=\"code\">@Profile<\/span> annotation lets us pass an array of strings, which can be seen in the annotation&#8217;s source code:<\/p>\n<pre><code class=\"java\">@Retention(RetentionPolicy.RUNTIME)\r\n@Target({ElementType.TYPE, ElementType.METHOD})\r\n@Documented\r\n@Conditional({ProfileCondition.class})\r\npublic @interface Profile {\r\n    String[] value();\r\n}<\/code><\/pre>\n<p>The result in this example is that the <span class=\"code\">noOpText<\/span> encryptor will be used when the active Spring profile is <i>development<\/i>, and the <span class=\"code\">queryableText<\/span> encryptor will be used when the active profile is either <i>staging<\/i> or <i>production<\/i>. I personally use Java configuration, but this can also be done with XML.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is going to be a quick one. I just want to show you how one can\u00a0define a bean in Spring Framework\u00a0for multiple profiles. That is, the bean will be &#8220;active&#8221; if the active profile is one in a given list. This is for example useful if you want different beans to be available at&hellip; <a href=\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles\" class=\"more-link\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[96],"tags":[42,98],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Enabling Spring Bean for Multiple Profiles<\/title>\n<meta name=\"description\" content=\"Enabling Spring beans depending on the active Spring profile is easy. And enabling a bean for multiple profiles is easier than you might initially think.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Enabling Spring Bean for Multiple Profiles\" \/>\n<meta property=\"og:description\" content=\"Enabling Spring beans depending on the active Spring profile is easy. And enabling a bean for multiple profiles is easier than you might initially think.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-13T22:20:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T15:38:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles\",\"url\":\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles\",\"name\":\"Enabling Spring Bean for Multiple Profiles\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2015-03-13T22:20:04+00:00\",\"dateModified\":\"2017-06-11T15:38:29+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Enabling Spring beans depending on the active Spring profile is easy. And enabling a bean for multiple profiles is easier than you might initially think.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Enabling Spring Bean for Multiple Profiles\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Enabling Spring Bean for Multiple Profiles","description":"Enabling Spring beans depending on the active Spring profile is easy. And enabling a bean for multiple profiles is easier than you might initially think.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles","og_locale":"en_US","og_type":"article","og_title":"Enabling Spring Bean for Multiple Profiles","og_description":"Enabling Spring beans depending on the active Spring profile is easy. And enabling a bean for multiple profiles is easier than you might initially think.","og_url":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2015-03-13T22:20:04+00:00","article_modified_time":"2017-06-11T15:38:29+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles","url":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles","name":"Enabling Spring Bean for Multiple Profiles","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2015-03-13T22:20:04+00:00","dateModified":"2017-06-11T15:38:29+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Enabling Spring beans depending on the active Spring profile is easy. And enabling a bean for multiple profiles is easier than you might initially think.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/java\/spring-framework\/enabling-spring-bean-for-multiple-profiles#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Enabling Spring Bean for Multiple Profiles"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-p3","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/1553"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=1553"}],"version-history":[{"count":4,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/1553\/revisions"}],"predecessor-version":[{"id":2873,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/1553\/revisions\/2873"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=1553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=1553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=1553"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=1553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}