{"id":21839,"date":"2014-02-21T10:00:06","date_gmt":"2014-02-21T08:00:06","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=21839"},"modified":"2014-02-20T10:35:55","modified_gmt":"2014-02-20T08:35:55","slug":"tutorial-writing-your-own-cdi-extension","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html","title":{"rendered":"Tutorial: Writing your own CDI extension"},"content":{"rendered":"<p>Today I will show you how to write a CDI extension.<\/p>\n<p>CDI provides a easy way for extending the functionality, like<\/p>\n<ul>\n<li>adding own scopes,<\/li>\n<li>enabling java core classes for extension,<\/li>\n<li>using the annotation meta data for augmentation or modification,<\/li>\n<li>and much more.<\/li>\n<\/ul>\n<p>In this tutorial we will implement an extension that will inject properties from a property file, as usual I will provide the sources at github [Update: <a href=\"https:\/\/github.com\/coders-kitchen\/Tutorial-PropertiesLoaderExtension\" target=\"_blank\">sources @ github<\/a>].<\/p>\n<h2>The goal<\/h2>\n<p>Providing an extension that allows us to do the following<\/p>\n<pre class=\"brush:java\">@PropertyFile(\"myProps.txt\")\r\npublic class MyProperties {\r\n  @Property(\"version\")\r\n  Integer version;\r\n \r\n  @Property(\"appname\")\r\n  String appname;\r\n}<\/pre>\n<p>where <em>version<\/em> and <em>appname<\/em> are defined in the file <em>myProps.txt<\/em>.<\/p>\n<h2>Preparation<\/h2>\n<p>At first we need the dependency of the CDI api<\/p>\n<pre class=\"brush:java\">dependencies.compile \"javax.enterprise:cdi-api:1.1-20130918\"<\/pre>\n<p>Now we can start. So let\u2019s<\/p>\n<h2>Getting wet<\/h2>\n<h3>The basics<\/h3>\n<p>The entry point for every CDI extension is a class that implements\u00a0\u00a0<em>javax.enterprise.inject.spi.Extension<\/em><\/p>\n<pre class=\"brush:java\">package com.coderskitchen.propertyloader;\r\n \r\nimport javax.enterprise.inject.spi.Extension;\r\npublic class PropertyLoaderExtension implements Extension {\r\n\/\/  More code later\r\n}<\/pre>\n<p>Additionally we must add the full qualified name of this class to a file named\u00a0<em>\u00a0javax.enterprise.inject.spi.Extension<\/em> in <em>META-INF\/services<\/em> directory.<\/p>\n<p><strong>javax.enterprise.inject.spi.Extension<\/strong><\/p>\n<pre class=\"brush:java\"> \t\r\ncom.coderskitchen.propertyloader.PropertyLoaderExtension<\/pre>\n<p>These are the basic steps for writing a CDI extension.<\/p>\n<blockquote>\n<p><strong>Background information<br \/>\n<\/strong>CDI uses Java SE\u2019s service provider architecture, that\u2019s why we need to implement the marker interface and adding the file with the FQN of the implementing class.<\/p>\n<\/blockquote>\n<h3>Diving deeper<\/h3>\n<p>Now we must choose the right event to listen for.<\/p>\n<blockquote>\n<p><strong>Background information<br \/>\n<\/strong>The CDI specs defines several events which are fired by the container during the initialization of the application.<br \/>\nFor example the\u00a0<em>BeforeBeanDiscovery<\/em> is fired before the container starts with the bean discovery.<\/p>\n<\/blockquote>\n<p>For this tutorial we need to listen for the\u00a0<em>ProcessInjectionTarget<\/em>\u00a0event. This event is fired for every single java class, interface or enum that is discovered and that is possibly instantiated by the container during runtime.<br \/>\nSo let\u2019s add the observer for this event:<\/p>\n<pre class=\"brush:java\">public &lt;T&gt; void initializePropertyLoading(final @Observes ProcessInjectionTarget&lt;T&gt; pit) {\r\n}<\/pre>\n<p>The\u00a0<em>ProcessInjectionTarget<\/em> grants access to the underlying class via the method\u00a0<em>getAnnotatedType<\/em> and the instance in creation via <em>getInjectionTarget.<\/em> We use the annotatedType for getting the annotations on the class to check if <em>@PropertyFile<\/em> is available.\u00a0If not, we will return directly as a short circuit.<\/p>\n<p>The <em>InjectionTarget<\/em> is later used for overwriting the current behavior and setting the values from the properties file.<\/p>\n<pre class=\"brush:java\">public &lt;T&gt; void initializePropertyLoading(final @Observes ProcessInjectionTarget&lt;T&gt; pit) {\r\n            AnnotatedType&lt;T&gt; at = pit.getAnnotatedType();\r\n            if(!at.isAnnotationPresent(PropertyFile.class)) {\r\n                    return;\r\n            }\r\n    }<\/pre>\n<p>For the sake of this tutorial we assume that the properties file is located directly in the root of the classpath. With this assumption we can add the following code to load the properties from the file<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">PropertyFile propertyFile = at.getAnnotation(PropertyFile.class);\r\nString filename = propertyFile.value();\r\nInputStream propertiesStream = getClass().getResourceAsStream(\"\/\" + filename);\r\nProperties properties = new Properties();\r\ntry {\r\n    properties.load(propertiesStream);\r\n    assignPropertiesToFields(at.getFields, properties); \/\/ Implementation follows\r\n} catch (IOException e) {\r\n    e.printStackTrace();\r\n}<\/pre>\n<p>Now we can assign the property values to the fields. But for CDI we have to do this in a slightly different way. We should use the\u00a0<em>InjectionTarget\u00a0<\/em>and override the current AnnotatedType. This allows CDI to ensure that all things could happen in a proper order.<\/p>\n<p>For achieving this we use a <em>final Map&lt;Field, Object&gt;<\/em> where we can store the current assignments for later usage in the <em>InjectionTarget<\/em>. The mapping is done in the method\u00a0<em>assignPropertiesToFields<\/em>.<\/p>\n<pre class=\"brush:java\">private &lt;T&gt; void assignPropertiesToFields(Set&lt;AnnotatedField&lt;? super T&gt;&gt; fields, Properties properties) {\r\n        for (AnnotatedField&lt;? super T&gt; field : fields) {\r\n            if(field.isAnnotationPresent(Property.class)) {\r\n                Property property = field.getAnnotation(Property.class);\r\n                String value = properties.getProperty(property.value());\r\n                Type baseType = field.getBaseType();\r\n                fieldValues.put(memberField, value);\r\n            }\r\n        }<\/pre>\n<p>As a last step we will now create a new InjectionTarget to assign the field values to all newly created instance of the underlying class.<\/p>\n<pre class=\"brush:java\">final InjectionTarget&lt;T&gt; it = pit.getInjectionTarget();\r\n    InjectionTarget&lt;T&gt; wrapped = new InjectionTarget&lt;T&gt;() {\r\n      @Override\r\n      public void inject(T instance, CreationalContext&lt;T&gt; ctx) {\r\n        it.inject(instance, ctx);\r\n        for (Map.Entry&lt;Field, Object&gt; property: fieldValues.entrySet()) {\r\n          try {\r\n            Field key = property.getKey();\r\n            key.setAccessible(true);\r\n            Class&lt;?&gt; baseType = key.getType();\r\n            String value = property.getValue().toString();\r\n            if (baseType == String.class) {\r\n              key.set(instance, value);\r\n            }  else if (baseType == Integer.class) {\r\n              key.set(instance, Integer.valueOf(value));\r\n            } else {\r\n              pit.addDefinitionError(new InjectionException(\"Type \" + baseType + \" of Field \" + key.getName() + \" not recognized yet!\"));\r\n            }\r\n          } catch (Exception e) {\r\n            pit.addDefinitionError(new InjectionException(e));\r\n          }\r\n        }\r\n      }\r\n \r\n      @Override\r\n      public void postConstruct(T instance) {\r\n        it.postConstruct(instance);\r\n      }\r\n \r\n      @Override\r\n      public void preDestroy(T instance) {\r\n        it.dispose(instance);\r\n      }\r\n \r\n      @Override\r\n      public void dispose(T instance) {\r\n        it.dispose(instance);\r\n      }\r\n \r\n      @Override\r\n      public Set&lt;InjectionPoint&gt; getInjectionPoints() {\r\n        return it.getInjectionPoints();\r\n      }\r\n \r\n      @Override\r\n      public T produce(CreationalContext&lt;T&gt; ctx) {\r\n        return it.produce(ctx);\r\n      }\r\n    };\r\n    pit.setInjectionTarget(wrapped);<\/pre>\n<p>That\u2019s all for doing the magic. Finally here is the complete code of the ProperyLoaderExtension.<br \/>\n<strong><br \/>\nPropertyLoaderExtension<\/strong><\/p>\n<pre class=\"brush:java\">package com.coderskitchen.propertyloader;\r\n \r\nimport javax.enterprise.context.spi.CreationalContext;\r\nimport javax.enterprise.event.Observes;\r\nimport javax.enterprise.inject.InjectionException;\r\nimport javax.enterprise.inject.spi.AnnotatedField;\r\nimport javax.enterprise.inject.spi.AnnotatedType;\r\nimport javax.enterprise.inject.spi.Extension;\r\nimport javax.enterprise.inject.spi.InjectionPoint;\r\nimport javax.enterprise.inject.spi.InjectionTarget;\r\nimport javax.enterprise.inject.spi.ProcessInjectionTarget;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.lang.reflect.Field;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\nimport java.util.Properties;\r\nimport java.util.Set;\r\n \r\npublic class PropertyLoaderExtension implements Extension {\r\n \r\n  final Map&lt;Field, Object&gt; fieldValues = new HashMap&lt;Field, Object&gt;();\r\n \r\n  public &lt;T&gt; void initializePropertyLoading(final @Observes ProcessInjectionTarget&lt;T&gt; pit) {\r\n    AnnotatedType&lt;T&gt; at = pit.getAnnotatedType();\r\n    if(!at.isAnnotationPresent(PropertyyFile.class)) {\r\n      return;\r\n    }\r\n    PropertyyFile propertyyFile = at.getAnnotation(PropertyyFile.class);\r\n    String filename = propertyyFile.value();\r\n    InputStream propertiesStream = getClass().getResourceAsStream(\"\/\" + filename);\r\n    Properties properties = new Properties();\r\n    try {\r\n      properties.load(propertiesStream);\r\n      assignPropertiesToFields(at.getFields(), properties);\r\n \r\n    } catch (IOException e) {\r\n      e.printStackTrace();\r\n    }\r\n \r\n    final InjectionTarget&lt;T&gt; it = pit.getInjectionTarget();\r\n    InjectionTarget&lt;T&gt; wrapped = new InjectionTarget&lt;T&gt;() {\r\n      @Override\r\n      public void inject(T instance, CreationalContext&lt;T&gt; ctx) {\r\n        it.inject(instance, ctx);\r\n        for (Map.Entry&lt;Field, Object&gt; property: fieldValues.entrySet()) {\r\n          try {\r\n            Field key = property.getKey();\r\n            key.setAccessible(true);\r\n            Class&lt;?&gt; baseType = key.getType();\r\n            String value = property.getValue().toString();\r\n            if (baseType == String.class) {\r\n              key.set(instance, value);\r\n            }  else if (baseType == Integer.class) {\r\n              key.set(instance, Integer.valueOf(value));\r\n            } else {\r\n              pit.addDefinitionError(new InjectionException(\"Type \" + baseType + \" of Field \" + key.getName() + \" not recognized yet!\"));\r\n            }\r\n          } catch (Exception e) {\r\n            pit.addDefinitionError(new InjectionException(e));\r\n          }\r\n        }\r\n      }\r\n \r\n      @Override\r\n      public void postConstruct(T instance) {\r\n        it.postConstruct(instance);\r\n      }\r\n \r\n      @Override\r\n      public void preDestroy(T instance) {\r\n        it.dispose(instance);\r\n      }\r\n \r\n      @Override\r\n      public void dispose(T instance) {\r\n        it.dispose(instance);\r\n      }\r\n \r\n      @Override\r\n      public Set&lt;InjectionPoint&gt; getInjectionPoints() {\r\n        return it.getInjectionPoints();\r\n      }\r\n \r\n      @Override\r\n      public T produce(CreationalContext&lt;T&gt; ctx) {\r\n        return it.produce(ctx);\r\n      }\r\n    };\r\n    pit.setInjectionTarget(wrapped);\r\n  }\r\n \r\n  private &lt;T&gt; void assignPropertiesToFields(Set&lt;AnnotatedField&lt;? super T&gt;&gt; fields, Properties properties) {\r\n    for (AnnotatedField&lt;? super T&gt; field : fields) {\r\n      if(field.isAnnotationPresent(Propertyy.class)) {\r\n        Propertyy propertyy = field.getAnnotation(Propertyy.class);\r\n        Object value = properties.get(propertyy.value());\r\n        Field memberField = field.getJavaMember();\r\n        fieldValues.put(memberField, value);\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<h2>Downloads<\/h2>\n<p>The complete source code will be available on git hub until Monday evening. The jar archive is available here\u00a0<a href=\"http:\/\/coders-kitchen.com\/wp-content\/uploads\/2014\/02\/PropertyLoaderExtension1.zip\">PropertyLoaderExtension<\/a>.<\/p>\n<h3>Final notes<\/h3>\n<p>This tutorial has shown how simply you can add a new features to the CDI framework. Within a few lines of code a working property loading and injection mechanism was added. The events that are fired during the lifecycles of an application provides a loose coupled and powerful approach to add new features, intercept bean creation or changing the behavior.<\/p>\n<p>The property injection could also be achieved by introducing a set of producer methods, but this approach requires one producer method per field type. With this general approach you just have to add new converters for enabling injection of other types of values.<br \/>\n&nbsp;<\/p>\n<div style=\"border: 1px solid #D8D8D8; background: #FAFAFA; width: 100%; padding-left: 5px;\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/coders-kitchen.com\/2014\/02\/02\/tutorial-writing-your-own-cdi-extension\/\">Tutorial: Writing your own CDI extension<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Peter Daum at the <a href=\"http:\/\/coders-kitchen.com\/\">Coders Kitchen<\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core classes for extension, using the annotation meta data for augmentation or modification, and much more. In this tutorial we will implement an extension that will inject properties from &hellip;<\/p>\n","protected":false},"author":538,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[290],"class_list":["post-21839","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-cdi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Tutorial: Writing your own CDI extension<\/title>\n<meta name=\"description\" content=\"Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial: Writing your own CDI extension\" \/>\n<meta property=\"og:description\" content=\"Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-02-21T08:00:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Peter Daum\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/MrPaeddah\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Peter Daum\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html\"},\"author\":{\"name\":\"Peter Daum\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/81841ee024f79c52d7358838084c9934\"},\"headline\":\"Tutorial: Writing your own CDI extension\",\"datePublished\":\"2014-02-21T08:00:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html\"},\"wordCount\":667,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"CDI\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html\",\"name\":\"Tutorial: Writing your own CDI extension\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2014-02-21T08:00:06+00:00\",\"description\":\"Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/02\\\/tutorial-writing-your-own-cdi-extension.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Tutorial: Writing your own CDI extension\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/81841ee024f79c52d7358838084c9934\",\"name\":\"Peter Daum\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0f11d29229b9cad73db8882e688e5215dcf9d18a445a71580cd1ef8f024f7f0e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0f11d29229b9cad73db8882e688e5215dcf9d18a445a71580cd1ef8f024f7f0e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0f11d29229b9cad73db8882e688e5215dcf9d18a445a71580cd1ef8f024f7f0e?s=96&d=mm&r=g\",\"caption\":\"Peter Daum\"},\"description\":\"Peter is senior Java developer in the telecommunication industry. He works in the area of business and operations support systems and is always happy to share his knowledge and experience. He is interested in everything related to Java and software craftsmanship.\",\"sameAs\":[\"http:\\\/\\\/coders-kitchen.com\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/MrPaeddah\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/peter-daum\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial: Writing your own CDI extension","description":"Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core","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:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html","og_locale":"en_US","og_type":"article","og_title":"Tutorial: Writing your own CDI extension","og_description":"Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core","og_url":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-02-21T08:00:06+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Peter Daum","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/MrPaeddah","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Peter Daum","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html"},"author":{"name":"Peter Daum","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/81841ee024f79c52d7358838084c9934"},"headline":"Tutorial: Writing your own CDI extension","datePublished":"2014-02-21T08:00:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html"},"wordCount":667,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["CDI"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html","url":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html","name":"Tutorial: Writing your own CDI extension","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2014-02-21T08:00:06+00:00","description":"Today I will show you how to write a CDI extension. CDI provides a easy way for extending the functionality, like adding own scopes, enabling java core","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/02\/tutorial-writing-your-own-cdi-extension.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Tutorial: Writing your own CDI extension"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/81841ee024f79c52d7358838084c9934","name":"Peter Daum","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/0f11d29229b9cad73db8882e688e5215dcf9d18a445a71580cd1ef8f024f7f0e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/0f11d29229b9cad73db8882e688e5215dcf9d18a445a71580cd1ef8f024f7f0e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0f11d29229b9cad73db8882e688e5215dcf9d18a445a71580cd1ef8f024f7f0e?s=96&d=mm&r=g","caption":"Peter Daum"},"description":"Peter is senior Java developer in the telecommunication industry. He works in the area of business and operations support systems and is always happy to share his knowledge and experience. He is interested in everything related to Java and software craftsmanship.","sameAs":["http:\/\/coders-kitchen.com\/","https:\/\/x.com\/https:\/\/twitter.com\/MrPaeddah"],"url":"https:\/\/www.javacodegeeks.com\/author\/peter-daum"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/21839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/538"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=21839"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/21839\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=21839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=21839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=21839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}