{"id":12133,"date":"2013-04-30T16:00:58","date_gmt":"2013-04-30T13:00:58","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=12133"},"modified":"2019-12-09T21:13:35","modified_gmt":"2019-12-09T19:13:35","slug":"java-ee-cdi-producer-methods-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html","title":{"rendered":"Java EE CDI Producer methods tutorial"},"content":{"rendered":"<p>This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we want to inject an object that is not itself a bean, when the concrete type of the object to be injected may vary at runtime, or when the object requires some custom initialization that the bean constructor does not perform. Producer methods allow us to expose any JDK class as a bean and define multiple beans, with different scopes or initialization, for the same implementation class. They also let us use runtime polymorphism with CDI.<\/p>\n<p>Here we will create a simple service with two implementations and then we will create a <code>Producer<\/code> method to produce and inject both implementations in an application.<\/p>\n<p>Our preferred development environment is <a href=\"http:\/\/www.eclipse.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Eclipse<\/a>. We are using Eclipse Juno (4.2) version, along with <a href=\"http:\/\/maven.apache.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maven<\/a> Integration plugin version 3.1.0. You can download Eclipse from <a href=\"http:\/\/www.eclipse.org\/downloads\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> and Maven Plugin for Eclipse from <a href=\"http:\/\/maven.apache.org\/eclipse-plugin.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. <a href=\"http:\/\/tomcat.apache.org\/download-70.cgi\" target=\"_blank\" rel=\"noopener noreferrer\">Tomcat 7<\/a> is the application server used.<\/p>\n<p>Let&#8217;s begin,<\/p>\n<h2>1. Create a new Maven project<\/h2>\n<p>Go to File -&gt; Project -&gt;Maven -&gt; Maven Project.<\/p>\n<p><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/New-Maven-Project.png\"><img decoding=\"async\" class=\"alignnone size-medium wp-image-4341\" alt=\"New-Maven-Project\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/06\/New-Maven-Project.png\"><\/a><\/p>\n<p>In the &#8220;Select project name and location&#8221; page of the wizard, make sure that &#8220;Create a simple project (skip archetype selection)&#8221; option is <strong>unchecked<\/strong>, hit &#8220;Next&#8221; to continue with default values.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-project.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-project.png\" alt=\"new project\" width=\"602\" height=\"547\" class=\"alignnone size-full wp-image-18290\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-project.png 602w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/new-project-300x272.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<p>Here the maven archetype for creating a web application must be added. Click on <strong>&#8220;Add Archetype&#8221;<\/strong> and add the archetype. Set the &#8220;Archetype Group Id&#8221; variable to <code>\"org.apache.maven.archetypes\"<\/code>, the &#8220;Archetype artifact Id&#8221; variable to <code>\"maven-archetype-webapp\"<\/code> and the &#8220;Archetype Version&#8221; to <code>\"1.0\"<\/code>. Click on <strong>&#8220;OK&#8221;<\/strong> to continue.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/maven-archetype-webapp.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/maven-archetype-webapp.png\" alt=\"maven-archetype-webapp\" width=\"660\" height=\"549\" class=\"alignnone size-full wp-image-18291\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/maven-archetype-webapp.png 660w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/07\/maven-archetype-webapp-300x249.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/a><\/p>\n<p>In the &#8220;Enter an artifact id&#8221; page of the wizard, you can define the name and main package of your project. Set the &#8220;Group Id&#8221; variable to <code>\"com.javacodegeeks.snippets.enterprise\"<\/code> and the &#8220;Artifact Id&#8221; variable to <code>\"cdibeans\"<\/code>. The aforementioned selections compose the main project package as <code>\"com.javacodegeeks.snippets.enterprise.cdibeans\"<\/code> and the project name as <code>\"cdibeans\"<\/code>. Set the &#8220;Package&#8221; variable to <code>\"war\"<\/code>, so that a war file will be created to be deployed to tomcat server. Hit &#8220;Finish&#8221; to exit the wizard and to create your project.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject1.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject1.png\" alt=\"newcdiproject1\" width=\"602\" height=\"542\" class=\"alignnone size-full wp-image-18913\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject1.png 602w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject1-300x270.png 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<p>The Maven project structure is shown below:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject2.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject2.png\" alt=\"newcdiproject2\" width=\"369\" height=\"396\" class=\"alignnone size-full wp-image-18914\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject2.png 369w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/newcdiproject2-279x300.png 279w\" sizes=\"(max-width: 369px) 100vw, 369px\" \/><\/a><\/p>\n<ul>It consists of the following folders:<\/p>\n<li>\/src\/main\/java folder, that contains source files for the dynamic content of the application,<\/li>\n<li>\/src\/test\/java folder contains all source files for unit tests,<\/li>\n<li>\/src\/main\/resources folder contains configurations files,<\/li>\n<li>\/target folder contains the compiled and packaged deliverables,<\/li>\n<li>\/src\/main\/resources\/webapp\/WEB-INF folder contains the deployment descriptors for the Web application ,<\/li>\n<li>the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.<\/li>\n<\/ul>\n<h2>2. Add all the necessary dependencies<\/h2>\n<p>You can add the dependencies in Maven\u2019s <code>pom.xml<\/code> file, by editing it at the &#8220;Pom.xml&#8221; page of the POM editor, as shown below:<br \/>\n&nbsp;<br \/>\n<span style=\"text-decoration: underline;\"><em>pom.xml:<\/em><\/span><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:xml\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;groupId&gt;com.javacodegeeks.snippets.enterprise.cdi&lt;\/groupId&gt;\n\t&lt;artifactId&gt;cdibeans&lt;\/artifactId&gt;\n\t&lt;packaging&gt;war&lt;\/packaging&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;name&gt;cdibeans Maven Webapp&lt;\/name&gt;\n\t&lt;url&gt;http:\/\/maven.apache.org&lt;\/url&gt;\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.jboss.weld.servlet&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;weld-servlet&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;1.1.10.Final&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;jstl&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;1.2&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;javax.servlet-api&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;3.0.1&lt;\/version&gt;\n\t\t\t&lt;scope&gt;provided&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.glassfish&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;javax.faces&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;2.1.7&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n\n\t&lt;build&gt;\n\t\t&lt;finalName&gt;cdibeans&lt;\/finalName&gt;\n\t&lt;\/build&gt;\n&lt;\/project&gt;\n<\/pre>\n<p>As you can see Maven manages library dependencies declaratively. A local repository is created (by default under {user_home}\/.m2 folder) and all required libraries are downloaded and placed there from public repositories. Furthermore intra \u2013 library dependencies are automatically resolved and manipulated.<\/p>\n<h2>3. Create a simple Service<\/h2>\n<p>The service that will be used in the <code>Producer<\/code> in this example is a simple service that creates a greeting message for the application that uses it. The <code>GreetingCard.java<\/code> class is an interface with a method that produces the greeting message.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingCard.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans;\n\npublic interface GreetingCard {\n\n\tvoid sayHello();\n}\n<\/pre>\n<p>The implementation of the service is shown below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingCardImpl.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans.impl;\n\nimport com.javacodegeeks.snippets.enterprise.cdibeans.GreetingCard;\n\npublic class GreetingCardImpl implements GreetingCard {\n\n\tpublic void sayHello() {\n\t\tSystem.out.println(\"Hello!!!\");\n\t}\n\n}\n<\/pre>\n<p>In order to show how polymorphism works in CDI we will create another implementation of the service, as shown below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AnotherGreetingCardImpl.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans.impl;\n\nimport com.javacodegeeks.snippets.enterprise.cdibeans.GreetingCard;\n\n\npublic class AnotherGreetingCardImpl implements GreetingCard {\n\n\tpublic void sayHello() {\n\t\tSystem.out.println(\"Have a nice day!!!\");\n\t}\n\n}\n<\/pre>\n<h2>4. Use of the Producer in a Managed Bean<\/h2>\n<p>In the managed bean below, we are making use of the <code>Producer<\/code> created above. In particular, in the  <code>GreetingCardFactory.java<\/code> class, we create a method, <code>getGreetingCard()<\/code>, annotated with the <code>@Produces<\/code> annotation. The method returns the correct implementation of the <code>GreetingCard<\/code> according to the <code>GreetingType<\/code> of the bean. In this way the polymorphism is supported.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingCardFactory.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans;\n\nimport java.io.Serializable;\n\nimport javax.enterprise.context.SessionScoped;\nimport javax.enterprise.inject.Produces;\n\nimport com.javacodegeeks.snippets.enterprise.cdibeans.impl.AnotherGreetingCardImpl;\nimport com.javacodegeeks.snippets.enterprise.cdibeans.impl.GreetingCardImpl;\n\n@SessionScoped\npublic class GreetingCardFactory implements Serializable {\n\n\tprivate static final long serialVersionUID = -44416514616012281L;\n\n\tprivate GreetingType greetingType;\n\n\t@Produces\n\tpublic GreetingCard getGreetingCard() {\n\n\t\tswitch (greetingType) {\n\t\tcase HELLO:\n\t\t\treturn new GreetingCardImpl();\n\t\tcase ANOTHER_HI:\n\t\t\treturn new AnotherGreetingCardImpl();\n\t\tdefault:\n\t\t\treturn new GreetingCardImpl();\n\t\t}\n\t}\n}\n<\/pre>\n<p>The <code>GreetingType.java<\/code> is an enumeration, as shown below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingType.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans;\n\npublic enum GreetingType {\n\n\tHELLO, HI;\n}\n<\/pre>\n<p>Another way to use polymorphism in CDI is through the <code>@Qualifier<\/code>. We can create our own annotation in CDI, and then use it to in the <code>Producer<\/code> method to get the correct implementation.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Greetings.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans;\n\nimport static java.lang.annotation.ElementType.FIELD;\nimport static java.lang.annotation.ElementType.TYPE;\nimport static java.lang.annotation.ElementType.METHOD;\nimport static java.lang.annotation.RetentionPolicy.RUNTIME;\nimport java.lang.annotation.Retention;\nimport java.lang.annotation.Target;\n\nimport javax.inject.Qualifier;\n\n@Qualifier\n@Retention(RUNTIME)\n@Target({ FIELD, TYPE, METHOD })\npublic @interface Greetings {\n\n\tGreetingType value();\n\n}\n<\/pre>\n<p>Now, the <code>GreetingCardFactory<\/code> can have a different <code>Producer<\/code> method for every implementation. The annotation we created is used after the <code>@Produces<\/code> annotation.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingCardFactory.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans;\n\nimport java.io.Serializable;\n\nimport javax.enterprise.context.SessionScoped;\nimport javax.enterprise.inject.Produces;\n\nimport com.javacodegeeks.snippets.enterprise.cdibeans.impl.AnotherGreetingCardImpl;\nimport com.javacodegeeks.snippets.enterprise.cdibeans.impl.GreetingCardImpl;\n\n\n@SessionScoped\npublic class GreetingCardFactory implements Serializable {\n\n\tprivate static final long serialVersionUID = -44416514616012281L;\n\n\tprivate GreetingType greetingType;\n\n\t\n\t@Produces\n\t@Greetings(GreetingType.HELLO)\n\tpublic GreetingCard getGreetingCard() {\n\t\treturn new GreetingCardImpl();\n\t}\n\t\n\t@Produces\n\t@Greetings(GreetingType.HI)\n\tpublic GreetingCard getAnotherGreetingCard() {\n\t\treturn new AnotherGreetingCardImpl();\n\t}\n\t\n}\n<\/pre>\n<h2>5. Scope of Producer<\/h2>\n<p>An important thing to notice when creating a <code>Producer<\/code> method is the scope of the <code>Producer<\/code>. The scope of the <code>Producer<\/code> is set to <code>Dependent<\/code> by default. But a <code>Producer<\/code> may also have any other scope according to its use. The problem is, what may happen if a <code>Producer<\/code> produces a managed bean of a different scope. For example, we can create a <code>@RequestScoped<\/code> bean and inject it to another bean, using a <code>Producer<\/code> annotated as <code>@SessionScoped<\/code>. In this case, the instance of the <code>@RequestScoped<\/code> bean that the <code>Producer<\/code> returns will be destroyed after the HTTP request finishes and the behaviour of the bean where it is injected might be unexpected. In order to avoid having reference to no longer existing beans, we can make use of the <code>@New<\/code> annotation when using a <code>Producer<\/code> method. In this way, a new <code>Dependent<\/code> instance is injected via the <code>Producer<\/code>, as shown in the code snippet below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingCardFactory.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans;\n\nimport java.io.Serializable;\n\nimport javax.enterprise.context.SessionScoped;\nimport javax.enterprise.inject.New;\nimport javax.enterprise.inject.Produces;\n\nimport com.javacodegeeks.snippets.enterprise.cdibeans.impl.GreetingCardImpl;\n\n\n@SessionScoped\npublic class GreetingCardFactory implements Serializable {\n\n\tprivate static final long serialVersionUID = -44416514616012281L;\n\t\n\tprivate GreetingCardImpl greetingCard;\n\t\n\t@Produces\n\tpublic GreetingCardImpl getGreetingCard(@New GreetingCardImpl greetingCard) {\n\t\treturn greetingCard;\n\t}\n\n\t\n}\n<\/pre>\n<h2>6. Run the application<\/h2>\n<p>In order to run the application, we have created a simple servlet, as shown below:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>GreetingServlet.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.snippets.enterprise.cdibeans.servlet;\n\nimport java.io.IOException;\nimport java.io.PrintWriter;\n\nimport javax.inject.Inject;\nimport javax.servlet.ServletException;\nimport javax.servlet.annotation.WebServlet;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\nimport com.javacodegeeks.snippets.enterprise.cdibeans.GreetingCard;\nimport com.javacodegeeks.snippets.enterprise.cdibeans.GreetingType;\nimport com.javacodegeeks.snippets.enterprise.cdibeans.Greetings;\n\n@WebServlet(name = \"greetingServlet\", urlPatterns = {\"\/sayHello\"})\npublic class GreetingServlet extends HttpServlet {\n\n\tprivate static final long serialVersionUID = 2280890757609124481L;\n\t\n\t@Inject\n\t@Greetings(GreetingType.HELLO)\n\tprivate GreetingCard greetingCard;\n\n\t@Inject\n\t@Greetings(GreetingType.HI)\n\tprivate GreetingCard anotherGreetingCard;\n\n\t  public void init() throws ServletException {\n\t  }\n\n\t  public void doGet(HttpServletRequest request, HttpServletResponse response)\n\t            throws ServletException, IOException {\n\t      response.setContentType(\"text\/html\");\n\t      PrintWriter out = response.getWriter();\n\t      out.println(\"&lt;h1&gt;\" + greetingCard.sayHello() + \"&lt;\/h1&gt;\");\n\t      out.println(\"&lt;h1&gt;\" + anotherGreetingCard.sayHello() + \"&lt;\/h1&gt;\");\n\t  }\n\t  \n\t  public void destroy(){\n\t  }\n\n}\n<\/pre>\n<p>In the servlet above both implementations are injected. To run the example we must build the project with Maven, and then place the <code>war<\/code> file produced in <code>webbaps<\/code> folder of tomcat. Then, we can hit on :<\/p>\n<p><code>http:\/\/localhost\/8080\/cdibeans\/sayHello<\/code><\/p>\n<p>and the result is the one shown below:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/prod.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/prod.png\" alt=\"prod\" width=\"531\" height=\"416\" class=\"alignnone size-full wp-image-19094\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/prod.png 531w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/prod-300x235.png 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/a><\/p>\n<p>&nbsp;<br \/>\nThis was a tutorial of Java EE CDI Producer methods.<br \/>\n&nbsp;<br \/>\nDownload the source code of this tutorial: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2013\/04\/CDIProducerMethodExample.zip\">CDIProducerMethodExample.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we want to inject an object that is not itself a bean, when the concrete type of the object to be injected may vary at runtime, or when &hellip;<\/p>\n","protected":false},"author":472,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[290,289],"class_list":["post-12133","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-cdi","tag-java-ee6"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java EE CDI Producer methods tutorial<\/title>\n<meta name=\"description\" content=\"This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we\" \/>\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\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java EE CDI Producer methods tutorial\" \/>\n<meta property=\"og:description\" content=\"This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.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=\"2013-04-30T13:00:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-09T19:13:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-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=\"Theodora Fragkouli\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Theodora Fragkouli\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html\"},\"author\":{\"name\":\"Theodora Fragkouli\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/6bdf5e6534337198ce06de139e9b617b\"},\"headline\":\"Java EE CDI Producer methods tutorial\",\"datePublished\":\"2013-04-30T13:00:58+00:00\",\"dateModified\":\"2019-12-09T19:13:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html\"},\"wordCount\":949,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"CDI\",\"Java EE6\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html\",\"name\":\"Java EE CDI Producer methods tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2013-04-30T13:00:58+00:00\",\"dateModified\":\"2019-12-09T19:13:35+00:00\",\"description\":\"This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/04\\\/java-ee-cdi-producer-methods-tutorial.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\":\"Java EE CDI Producer methods tutorial\"}]},{\"@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\\\/6bdf5e6534337198ce06de139e9b617b\",\"name\":\"Theodora Fragkouli\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/50acde9ff30ae7c7473f7581747f7cdc58aa0156e56bf0a578adaa59ca93ae73?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/50acde9ff30ae7c7473f7581747f7cdc58aa0156e56bf0a578adaa59ca93ae73?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/50acde9ff30ae7c7473f7581747f7cdc58aa0156e56bf0a578adaa59ca93ae73?s=96&d=mm&r=g\",\"caption\":\"Theodora Fragkouli\"},\"description\":\"Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/theodora-fragkouli\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java EE CDI Producer methods tutorial","description":"This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we","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\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Java EE CDI Producer methods tutorial","og_description":"This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we","og_url":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-04-30T13:00:58+00:00","article_modified_time":"2019-12-09T19:13:35+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Theodora Fragkouli","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Theodora Fragkouli","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html"},"author":{"name":"Theodora Fragkouli","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/6bdf5e6534337198ce06de139e9b617b"},"headline":"Java EE CDI Producer methods tutorial","datePublished":"2013-04-30T13:00:58+00:00","dateModified":"2019-12-09T19:13:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html"},"wordCount":949,"commentCount":4,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["CDI","Java EE6"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html","name":"Java EE CDI Producer methods tutorial","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2013-04-30T13:00:58+00:00","dateModified":"2019-12-09T19:13:35+00:00","description":"This is a tutorial of CDI Producer methods. In CDI, a Producer method generates an object that can then be injected. Producer methods can be used when we","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/04\/java-ee-cdi-producer-methods-tutorial.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":"Java EE CDI Producer methods tutorial"}]},{"@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\/6bdf5e6534337198ce06de139e9b617b","name":"Theodora Fragkouli","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/50acde9ff30ae7c7473f7581747f7cdc58aa0156e56bf0a578adaa59ca93ae73?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/50acde9ff30ae7c7473f7581747f7cdc58aa0156e56bf0a578adaa59ca93ae73?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/50acde9ff30ae7c7473f7581747f7cdc58aa0156e56bf0a578adaa59ca93ae73?s=96&d=mm&r=g","caption":"Theodora Fragkouli"},"description":"Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/theodora-fragkouli"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/12133","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\/472"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=12133"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/12133\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=12133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=12133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=12133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}