{"id":3597,"date":"2011-07-13T12:05:14","date_gmt":"2011-07-13T10:05:14","guid":{"rendered":"http:\/\/blog.jteam.nl\/?p=3597"},"modified":"2011-07-13T12:05:14","modified_gmt":"2011-07-13T10:05:14","slug":"new-in-spring-3-bean-definition-profiles-in-a-glimpse","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/","title":{"rendered":"New in Spring 3.1: Bean definition profiles at a glimpse"},"content":{"rendered":"<p>The next major release of Spring Framework, 3.1, brings a new feature called bean definition profiles. This is a great add-on which makes the definition of the application context even easier, both in xml and Java-based style. With this new functionality it is possible to group beans into profiles which can be activated at runtime. I will show in this blog post how to leverage bean definition profiles, based on a monitoring use case.<\/p>\n<p><!--more--><br \/>\nBean definition profiles where designed as a core mechanism allowing the use of different bean definitions in different circumstances. These different circumstances are usually a deployment on a different environment. It is quite a common need to have some beans defined when the application is deployed on a test environment and other ones when in acceptance or production. Stubbing external services, datasource definition (local vs. JNDI), notification mechanisms, just to name a few.<\/p>\n<p>A profile is defined using the &#8220;profile&#8221; property of the &#8220;beans&#8221; element in the context definition. The rules are quite simple: Beans not belonging to any profile are always available in the application context. Beans defined in a profile are only included in the application context if the given profile is active. The profile(s) can be defined in the top-level beans element of the context definition or in an embedded beans element [2]. It is also possible to define multiple profiles at once [3]. Additionally the @Profile annotation can be used in @Configuration classes.<\/p>\n<pre><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\napplicationContext-dev.xml &#x5B;1]\n&lt;beans profile=&quot;dev&quot;&gt;\n\t\/\/ beans\n&lt;\/bean&gt;\n\napplicationContext.xml &#x5B;2]\n&lt;beans&gt;\n       \/\/ beans\n    &lt;beans profile=&quot;dev&quot;&gt;\n       \/\/ beans\n    &lt;\/beans&gt;\n    &lt;beans profile=&quot;prod&quot;&gt;\n       \/\/ beans\n    &lt;\/beans&gt;\n&lt;\/beans&gt;\n\napplicationContext-dev-acceptance.xml&#x5B;3]\n&lt;beans profile=&quot;dev,acceptance&quot;&gt;\n      \/\/ beans\n&lt;\/bean&gt;\n<\/pre>\n<p>Let&#8217;s analyze how we can leverage bean definition profiles in a real-life use case. We will spy on an application&#8217;s <code>@Controller<\/code>s to see how long it takes to handle the incoming requests. For the sake of this example let&#8217;s assume that logging of the times is enough. First, let&#8217;s write an aspect to intercept the method invocation and log the execution time:<\/p>\n<pre><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class ControllerAspect {\n\nprivate static Logger logger = Logger.getLogger(&quot;monitor&quot;);\n\n@Pointcut(&quot;within(@org.springframework.stereotype.Controller *)&quot;)\npublic void controllerBean() {\n}\n\n@Pointcut(&quot;execution(* *(..))&quot;)\npublic void methodPointcut() {\n}\n\n@Around(&quot;controllerBean() &amp;&amp; methodPointcut()&quot;)\npublic Object aroundControllerMethods(ProceedingJoinPoint pjp) throws Throwable {\n     String signature = pjp.getSignature().toShortString();\n     StopWatch stopWatch = new StopWatch(signature);\n     stopWatch.start();\n     Object retVal = pjp.proceed();\n     stopWatch.stop();\n     logger.info(stopWatch.shortSummary());\n     return retVal;\n    }\n}\n<\/pre>\n<p>We also need a controller to spy on. Let&#8217;s use this trivial implementation of an encoding controller:<\/p>\n<pre><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Controller\npublic class EncodingController {\n\nprivate static final List&lt;String&gt; algorithms = Arrays.asList(&quot;MD5&quot;, &quot;SHA&quot;);\n\n@RequestMapping(value = &quot;algorithms&quot;, method = RequestMethod.GET)\npublic ResponseEntity&lt;String&gt; supportedAlgorithms() throws InterruptedException, UnsupportedEncodingException, NoSuchAlgorithmException {\n    return new ResponseEntity&lt;String&gt;(&quot;Supporter algorithms: &quot; + algorithms.toString() , HttpStatus.OK);\n}\n\n@RequestMapping(value = &quot;encode&quot;, method = RequestMethod.GET)\npublic ResponseEntity&lt;String&gt;; encode(@RequestParam(&quot;value&quot;) String value, @RequestParam(&quot;algorithm&quot;) String algorithm) throws InterruptedException, UnsupportedEncodingException, NoSuchAlgorithmException {\n     if (!algorithms.contains(algorithm)) {\n          return new ResponseEntity&lt;String&gt;(&quot;Algorithm not supported&quot;, HttpStatus.UNPROCESSABLE_ENTITY);\n     }\n    MessageDigest md = MessageDigest.getInstance(algorithm);\n    byte&#x5B;] digest = md.digest(value.getBytes());\n    return new ResponseEntity&lt;String&gt;(String.valueOf(Hex.encodeHex(digest)), HttpStatus.OK);\n    }\n}<\/pre>\n<p>Once our aspect and controller code is ready we can move to defining a separate profile for the monitoring. We expect the monitoring to be on only in certain cases, for instance when deployed on a performance testing environment. This is where the bean definition profiles come into play. We will define the aspect in a separate profile which can be activated at runtime:<\/p>\n<pre><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;beans profile=&quot;monitoring&quot;&gt;\n    &lt;bean id=&quot;controllerAspect&quot; class=&quot;nl.jteam.sample.aop.ControllerAspect&quot;\/&gt;\n\n    &lt;aop:aspectj-autoproxy&gt;\n        &lt;aop:include name=&quot;controllerAspect&quot;\/&gt;\n    &lt;\/aop:aspectj-autoproxy&gt;\n\n&lt;\/beans&gt;\n<\/pre>\n<p>The &#8220;monitoring&#8221; profile can be easily activated by  passing -Dspring.profiles.active=&#8221;monitoring&#8221; parameter to the Java VM. More than one profile can be activated by specifying a coma-separated list, eg. -Dspring.profiles.active=&#8221;profile1,profile2,profile3&#8243;. When running the application with the &#8220;monitoring&#8221; profile we should notice logs upon each request:<\/p>\n<pre>\nINFO [monitor] - &lt;StopWatch 'EncodingController.supportedAlgorithms()':\n                               running time (millis) = 1&gt;\nINFO [monitor] - &lt;StopWatch 'EncodingController.encode(..)':\n                               running time (millis) = 29&gt;\n<\/pre>\n<p><strong>To keep in mind:<\/strong><br \/>\n&#8211; Do not use the profiles when the difference between the beans lies in properties. The <code>PropertyPlaceholderConfigurer<\/code> will do the job.<br \/>\n&#8211; Watch out for any functionality which could compromise your application when accidentally activated on a production environment.<\/p>\n<p>A working project with the code from the above example can be cloned from github: https:\/\/github.com\/annagos\/beanprofiles-monitoring-sample<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The next major release of Spring Framework, 3.1, brings a new feature called bean definition profiles. This is a great add-on which makes the definition of the application context even easier, both in xml and Java-based style. With this new functionality it is possible to group beans into profiles which can be activated at runtime. [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[31,10],"tags":[260,70,201,95],"class_list":["post-3597","post","type-post","status-publish","format-standard","hentry","category-java","category-development","tag-bean-profiles","tag-spring","tag-spring-3","tag-spring-framework"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>New in Spring 3.1: Bean definition profiles at a glimpse - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"New in Spring 3.1: Bean definition profiles at a glimpse - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"The next major release of Spring Framework, 3.1, brings a new feature called bean definition profiles. This is a great add-on which makes the definition of the application context even easier, both in xml and Java-based style. With this new functionality it is possible to group beans into profiles which can be activated at runtime. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-07-13T10:05:14+00:00\" \/>\n<meta name=\"author\" content=\"Allard Buijze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Allard Buijze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/\",\"url\":\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/\",\"name\":\"New in Spring 3.1: Bean definition profiles at a glimpse - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"datePublished\":\"2011-07-13T10:05:14+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4e232a11dc57a2c4c581956ce6fde63\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"New in Spring 3.1: Bean definition profiles at a glimpse\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4e232a11dc57a2c4c581956ce6fde63\",\"name\":\"Allard Buijze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3e7130b2465615e105b1addd400a3f06?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3e7130b2465615e105b1addd400a3f06?s=96&d=mm&r=g\",\"caption\":\"Allard Buijze\"},\"description\":\"Allard (@allardbz) is CTO of Trifork Amsterdam and founder of Axon Framework. He is a trainer and speaker at conferences on topics related to scalable architecture and domain driven design. He strongly believes that good craftsmanship can only be achieved through continuous and intensive exchange of experience with others. The last years, he has been investigating and applying CQRS to a number of projects. As a result, he created the Axon Framework, an open source Java framework that helps developers create scalable and extensible applications. Axon has a growing community and has already been successfully introduced in several high-profile projects around the world.\",\"url\":\"https:\/\/trifork.nl\/blog\/author\/allard\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"New in Spring 3.1: Bean definition profiles at a glimpse - Trifork Blog","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:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/","og_locale":"en_US","og_type":"article","og_title":"New in Spring 3.1: Bean definition profiles at a glimpse - Trifork Blog","og_description":"The next major release of Spring Framework, 3.1, brings a new feature called bean definition profiles. This is a great add-on which makes the definition of the application context even easier, both in xml and Java-based style. With this new functionality it is possible to group beans into profiles which can be activated at runtime. [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/","og_site_name":"Trifork Blog","article_published_time":"2011-07-13T10:05:14+00:00","author":"Allard Buijze","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Allard Buijze","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/","url":"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/","name":"New in Spring 3.1: Bean definition profiles at a glimpse - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"datePublished":"2011-07-13T10:05:14+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4e232a11dc57a2c4c581956ce6fde63"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/new-in-spring-3-bean-definition-profiles-in-a-glimpse\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"New in Spring 3.1: Bean definition profiles at a glimpse"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4e232a11dc57a2c4c581956ce6fde63","name":"Allard Buijze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3e7130b2465615e105b1addd400a3f06?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3e7130b2465615e105b1addd400a3f06?s=96&d=mm&r=g","caption":"Allard Buijze"},"description":"Allard (@allardbz) is CTO of Trifork Amsterdam and founder of Axon Framework. He is a trainer and speaker at conferences on topics related to scalable architecture and domain driven design. He strongly believes that good craftsmanship can only be achieved through continuous and intensive exchange of experience with others. The last years, he has been investigating and applying CQRS to a number of projects. As a result, he created the Axon Framework, an open source Java framework that helps developers create scalable and extensible applications. Axon has a growing community and has already been successfully introduced in several high-profile projects around the world.","url":"https:\/\/trifork.nl\/blog\/author\/allard\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/3597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=3597"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/3597\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=3597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=3597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=3597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}