{"id":4373,"date":"2012-02-22T17:08:46","date_gmt":"2012-02-22T22:08:46","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=4373"},"modified":"2012-04-06T17:49:07","modified_gmt":"2012-04-06T22:49:07","slug":"cdi-reference-implementation-weld","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=4373","title":{"rendered":"CDI Reference Implementation: WELD"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Download_WELD\">Download WELD<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Example\">Example<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Setup_Eclipse_Project\">Setup Eclipse Project<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Create_Java_Classes\">Create Java Classes<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Create_a_Test_JSF_Page\">Create a Test JSF Page<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Deploy_and_Test\">Deploy and Test<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Deploy_to_Tomcat_6_or_7\">Deploy to Tomcat 6 or 7<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Deploy_to_WebLogic_11g_R1\">Deploy to WebLogic 11g R1<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#Conversational_Scope\">Conversational Scope<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#CDI_JUnit_Testing\">CDI JUnit Testing<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Sample_Code\">Sample Code<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Issues\">Issues<\/a>\n\t\t<ol class='toc-even level-2'>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Singleton_is_not_set_when_Open_JSF_Page\"> Singleton is not set when Open JSF Page<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#WELD-000101_Transactional_services_not_available\">WELD-000101 Transactional services not available<\/a>\n\t\t\t<\/li>\n\t\t\t<li>\n\t\t\t\t<a href=\"#Use_Injected_Beans_in_Constructor\">Use Injected Beans in Constructor<\/a>\n\t\t\t<\/li>\n\t\t<\/ol>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Download_WELD\"><h2>Download WELD<\/h2><\/span>\n<p>* Download Weld from <a href=\"http:\/\/seamframework.org\/Weld\/Downloads\">http:\/\/seamframework.org\/Weld\/Downloads<\/a><br \/>\n* I downloaded <em>weld-1.1.5.Final.zip<\/em><br \/>\n* Unzip into a local directory, e.g. <em>c:\\prog<\/em><\/p>\n<span id=\"Example\"><h2>Example<\/h2><\/span>\n<span id=\"Setup_Eclipse_Project\"><h3>Setup Eclipse Project<\/h3><\/span>\n<p>* Start Eclipse, I used <em>Indigo<\/em><br \/>\n* Create a new <em>Dynamic Web Project<\/em> named: <strong>test.cdi<\/strong><br \/>\n* Click <strong>Finish <\/strong>button to create project<\/p>\n<span id=\"\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj.jpg\" alt=\"\" title=\"weld_hello_proj\" width=\"286\" height=\"414\" class=\"aligncenter size-full wp-image-4427\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj.jpg 286w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj-207x300.jpg 207w\" sizes=\"auto, (max-width: 286px) 100vw, 286px\" \/><\/a><\/h6><\/span>\n<span id=\"_1\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_name.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_name-252x300.jpg\" alt=\"\" title=\"weld_hello_proj_name\" width=\"252\" height=\"300\" class=\"aligncenter size-medium wp-image-4428\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_name-252x300.jpg 252w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_name.jpg 602w\" sizes=\"auto, (max-width: 252px) 100vw, 252px\" \/><\/a><\/h6><\/span>\n<p>* Add <strong>weld-servlet.jar<\/strong> to project buildpath<em><br \/>\n* Also add <em>MyFaces Core 2.0.11<\/em> and <\/em>MyFaces Trinidad 2.0.0 user libraries to build path. See <a href=\"?p=3813\">this post<\/a> for how to setup these user libraries.<\/p>\n<span id=\"_2\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_buildpath.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_buildpath-300x229.jpg\" alt=\"\" title=\"weld_hello_proj_buildpath\" width=\"300\" height=\"229\" class=\"aligncenter size-medium wp-image-4429\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_buildpath-300x229.jpg 300w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_buildpath.jpg 827w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/h6><\/span>\n<p>* Add both <em>weld-servlet.jar<\/em>, <em>MyFaces Core 2.0.11<\/em>, and <em>MyFaces Trinidad 2.0.0<\/em> user libraries to <em>Deployment Assembly<\/em><\/p>\n<span id=\"_3\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly-300x230.jpg\" alt=\"\" title=\"weld_hello_proj_deployassembly\" width=\"300\" height=\"230\" class=\"aligncenter size-medium wp-image-4430\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly-300x230.jpg 300w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly.jpg 825w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/h6><\/span>\n<span id=\"_4\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly2-300x118.jpg\" alt=\"\" title=\"weld_hello_proj_deployassembly2\" width=\"300\" height=\"118\" class=\"aligncenter size-medium wp-image-4431\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly2-300x118.jpg 300w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_proj_deployassembly2.jpg 574w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/h6><\/span>\n<p>* Add to <em>web.xml<\/em><\/p>\n<pre lang=\"xml\">\r\n<listener> \r\n  <listener-class>org.jboss.weld.environment.servlet.Listener<\/listener-class> \r\n<\/listener>\r\n<\/pre>\n<p>* Also add to <em>web.xml<\/em> to enable JSF feature<\/p>\n<pre lang=\"xml\">\r\n\t<servlet>\r\n\t\t<servlet-name>JSF<\/servlet-name>\r\n\t\t<servlet-class>javax.faces.webapp.FacesServlet<\/servlet-class>\r\n\t<\/servlet>\r\n\t<servlet-mapping>\r\n\t\t<servlet-name>JSF<\/servlet-name>\r\n\t\t<url-pattern>\/faces\/*<\/url-pattern>\r\n\t<\/servlet-mapping>\r\n<\/pre>\n<p>* Final <em>web.xml<\/em> looks like this:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<web-app xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n\txmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\" xmlns:web=\"http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd\"\r\n\txsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd\"\r\n\tid=\"WebApp_ID\" version=\"2.5\">\r\n\t<display-name>test.cdi<\/display-name>\r\n\t<welcome-file-list>\r\n\t\t<welcome-file>index.html<\/welcome-file>\r\n\t\t<welcome-file>index.htm<\/welcome-file>\r\n\t\t<welcome-file>index.jsp<\/welcome-file>\r\n\t\t<welcome-file>default.html<\/welcome-file>\r\n\t\t<welcome-file>default.htm<\/welcome-file>\r\n\t\t<welcome-file>default.jsp<\/welcome-file>\r\n\t<\/welcome-file-list>\r\n\r\n\t<servlet>\r\n\t\t<servlet-name>JSF<\/servlet-name>\r\n\t\t<servlet-class>javax.faces.webapp.FacesServlet<\/servlet-class>\r\n\t<\/servlet>\r\n\t<servlet-mapping>\r\n\t\t<servlet-name>JSF<\/servlet-name>\r\n\t\t<url-pattern>\/faces\/*<\/url-pattern>\r\n\t<\/servlet-mapping>\r\n\r\n\t<listener>\r\n\t\t<listener-class>org.jboss.weld.environment.servlet.Listener<\/listener-class>\r\n\t<\/listener>\r\n\r\n<\/web-app>\r\n<\/pre>\n<p>* Create a new folder named <strong>META-INF<\/strong> under <em>src<\/em> directory.<br \/>\n* Create an <em>empty<\/em> <strong>beans.xml<\/strong> in the newly created <em>src\/META-INF<\/em> directory<br \/>\n<strong>Important note:<\/strong> you have to place <em>beans.xml<\/em> in <em>src\/META-INF<\/em> in order for WELD to work in WebLogic 10.3.5. Placing <strong>beans.xml<\/strong> in <em>WEB-INF<\/em> directory works for Tomcat and JBoss but <strong>not<\/strong> WebLogic 10.3.5.<\/p>\n<span id=\"Create_Java_Classes\"><h3>Create Java Classes<\/h3><\/span>\n<p>* Create a new package named <strong>hello<\/strong><br \/>\n* Create a new Java class named <strong>Greeting1<\/strong> with class annotation of <em>@Named<\/em>:<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\nimport javax.inject.Named;\r\n\r\n@Named\r\npublic class Greeting1 {\r\n\t\r\n\tpublic String getGreet() {\r\n\t\treturn \"Hello, my friend, without injection\";\r\n\t}\r\n}\r\n<\/pre>\n<p>* Create another Java class named Greeting2 without annotation:<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\npublic class Greeting2 {\r\n\r\n\tpublic String getGreet(String name){\r\n\t\treturn \"Hello, \" + name + \", from injection.\";\r\n\t}\r\n}\r\n<\/pre>\n<p>* Create a new Java class named <strong>GreetingService<\/strong> with class annotation of <em>@Named<\/em>. Also annotate to inject <em>Greeting2<\/em> to <em>GreetingService<\/em> as shown below:<\/p>\n<pre lang=\"java\">\r\npackage hello;\r\n\r\nimport javax.inject.Inject;\r\nimport javax.inject.Named;\r\n\r\n@Named\r\npublic class GreetingService {\r\n\r\n\t@Inject Greeting2 greeting;\r\n\tpublic String getGreet(){\r\n\t\treturn this.greeting.getGreet(\"my friend\");\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<span id=\"Create_a_Test_JSF_Page\"><h3>Create a Test JSF Page<\/h3><\/span>\n<p>* Create a test JSF page named <strong>hello.xhtml<\/strong><\/p>\n<pre lang=\"xml\">\r\n<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\">\r\n<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"\r\n\t  xmlns:h=\"http:\/\/java.sun.com\/jsf\/html\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=ISO-8859-1\"\/>\r\n<title>Insert title here<\/title>\r\n<\/head>\r\n<body>\r\n<b>Greet without injection:<\/b>\r\n#{greeting1.greet}\r\n\r\n<br\/>\r\n<b>Greet from injection:<\/b>\r\n#{greetingService.greet}\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n<span id=\"Deploy_and_Test\"><h2>Deploy and Test<\/h2><\/span>\n<p>* Project files:<\/p>\n<span id=\"_5\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_files.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_files.jpg\" alt=\"\" title=\"weld_hello_files\" width=\"299\" height=\"384\" class=\"aligncenter size-full wp-image-4455\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_files.jpg 299w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_files-233x300.jpg 233w\" sizes=\"auto, (max-width: 299px) 100vw, 299px\" \/><\/a><\/h6><\/span>\n<span id=\"Deploy_to_Tomcat_6_or_7\"><h3>Deploy to Tomcat 6 or 7<\/h3><\/span>\n<p>* Deploy to Tomcat 6 or 7<\/p>\n<span id=\"_6\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_add_to_tomcat.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_add_to_tomcat-289x300.jpg\" alt=\"\" title=\"weld_hello_add_to_tomcat\" width=\"289\" height=\"300\" class=\"aligncenter size-medium wp-image-4454\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_add_to_tomcat-289x300.jpg 289w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_add_to_tomcat.jpg 522w\" sizes=\"auto, (max-width: 289px) 100vw, 289px\" \/><\/a><\/h6><\/span>\n<p>* Point browser to <a href=\"http:\/\/localhost:8080\/test.cdi\/faces\/hello.xhtml\">http:\/\/localhost:8080\/test.cdi\/faces\/hello.xhtml<\/a>. You should see both greetings with and without injection.<\/p>\n<span id=\"_7\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_test.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_test.jpg\" alt=\"\" title=\"weld_hello_test\" width=\"406\" height=\"97\" class=\"aligncenter size-full wp-image-4425\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_test.jpg 406w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_test-300x71.jpg 300w\" sizes=\"auto, (max-width: 406px) 100vw, 406px\" \/><\/a><\/h6><\/span>\n<span id=\"Deploy_to_WebLogic_11g_R1\"><h3>Deploy to WebLogic 11g R1<\/h3><\/span>\n<p>* You have to place <em>beans.xml<\/em> in <em>src\/META-INF<\/em>. Place it in <em>WEB-INF<\/em> won&#8217;t work.<br \/>\n* You have to create a <strong>weblogic.xml<\/strong> in <em>WEB-INF<\/em> directory and set <em>wls:prefer-web-inf-classes<\/em> to <strong>true<\/strong>. Otherwise, <em>WEB-INF\/lib<\/em> jar files won&#8217;t get loaded properly.<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<wls:weblogic-web-app xmlns:wls=\"http:\/\/xmlns.oracle.com\/weblogic\/weblogic-web-app\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/ejb-jar_3_0.xsd http:\/\/xmlns.oracle.com\/weblogic\/weblogic-web-app http:\/\/xmlns.oracle.com\/weblogic\/weblogic-web-app\/1.2\/weblogic-web-app.xsd\">\r\n    <wls:weblogic-version>10.3.5<\/wls:weblogic-version>\r\n    <wls:context-root>test<\/wls:context-root>\r\n    <wls:container-descriptor>\r\n        <wls:prefer-web-inf-classes>true<\/wls:prefer-web-inf-classes>\r\n    <\/wls:container-descriptor>    \r\n<\/wls:weblogic-web-app>\r\n<\/pre>\n<p>* If you have third party jar files that are used in CDI injection, you need to add <em>beans.xml<\/em> to <em>META-INF<\/em> directory of the jar file in order for the jar file to be scanned by WebLogic for CDI bean classes.<br \/>\n* beans.xml needs to have the content:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<beans xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\"\r\n   xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n   xsi:schemaLocation=\"\r\n      http:\/\/java.sun.com\/xml\/ns\/javaee \r\n      http:\/\/java.sun.com\/xml\/ns\/javaee\/beans_1_0.xsd\">\r\n<\/beans>\r\n<\/pre>\n<p>* I deployed to JDev 11gR1 integrated WLS directly from Eclipse Indigo.<br \/>\n* Once deployed, point browser to <a href=\"http:\/\/localhost:7101\/test\/faces\/hello.xhtml\">http:\/\/localhost:7101\/test\/faces\/hello.xhtml<\/a><\/p>\n<span id=\"_8\"><h6><a href=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_wls_test.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_wls_test.jpg\" alt=\"\" title=\"weld_hello_wls_test\" width=\"409\" height=\"98\" class=\"aligncenter size-full wp-image-4453\" srcset=\"https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_wls_test.jpg 409w, https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/weld_hello_wls_test-300x71.jpg 300w\" sizes=\"auto, (max-width: 409px) 100vw, 409px\" \/><\/a><\/h6><\/span>\n<span id=\"Conversational_Scope\"><h2>Conversational Scope<\/h2><\/span>\n<pre lang=\"java\">\r\n@Named(\"usrMsgs\")\r\n@ConversationScoped\r\npublic class UserMessagesVO implements Serializable {\r\n\t@Inject\r\n\tConversation conversation;\r\n\r\n\tprivate void reload(){\r\n\t\tthis.endConversation();\r\n\t\tthis.beginConversation();\r\n\t\t\r\n\t\tthis.setupUsrAccount();\r\n\t}\r\n\r\n\tpublic void beginConversation() {\r\n\t\tif (conversation.isTransient()) {\r\n\t\t\tconversation.begin();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void endConversation() {\r\n\t\tif (!conversation.isTransient()) {\r\n\t\t\tconversation.end();\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<span id=\"CDI_JUnit_Testing\"><h2>CDI JUnit Testing<\/h2><\/span>\n<p>* Create a new Java class:<\/p>\n<pre lang=\"java\">\r\npackage test.util;\r\n\r\nimport org.jboss.weld.environment.se.Weld;\r\nimport org.jboss.weld.environment.se.WeldContainer;\r\nimport org.junit.runners.BlockJUnit4ClassRunner;\r\nimport org.junit.runners.model.InitializationError;\r\n\r\npublic class WeldJUnit4Runner extends BlockJUnit4ClassRunner {\r\n\tprivate Class<?> klass;\r\n\tprivate Weld weld;\r\n\tprivate WeldContainer container;\r\n\r\n\tpublic WeldJUnit4Runner(Class<?> klass) throws InitializationError {\r\n\t\tsuper(klass);\r\n\t\tthis.klass = klass;\r\n\t\tthis.weld = new Weld();\r\n\t\tthis.container = weld.initialize();\r\n\t}\r\n\r\n\t@Override\r\n\tprotected Object createTest() throws Exception {\r\n\t\treturn this.container.instance().select(this.klass).get();\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>* Annotate test class<\/p>\n<pre lang=\"java\">\r\n@RunWith(WeldJUnit4Runner.class)\r\npublic class TestNotifications {\r\n}\r\n<\/pre>\n<span id=\"Sample_Code\"><h2>Sample Code<\/h2><\/span>\n<p>* Sample code for this blog can be downloaded: <a href='https:\/\/jianmingli.com\/wp\/wp-content\/uploads\/2012\/02\/test.cdi_.zip'>test.cdi<\/a>.<\/p>\n<span id=\"Issues\"><h2>Issues<\/h2><\/span>\n<span id=\"Singleton_is_not_set_when_Open_JSF_Page\"><h3> Singleton is not set when Open JSF Page<\/h3><\/span>\n<p>* Error message:<\/p>\n<pre lang=\"bash\">\r\nCaused by:\r\njava.lang.IllegalStateException - Singleton is not set\r\nat org.jboss.weld.bootstrap.api.helpers.IsolatedStaticSingletonProvider$IsolatedStaticSingleton.get(IsolatedStaticSingletonProvider.java:52)\r\n<\/pre>\n<p>* Possible cause: missing weld listener in web.xml<br \/>\n* Fix: add to web.xml<\/p>\n<pre lang=\"xml\">\r\n  <listener>\r\n    <listener-class>org.jboss.weld.environment.servlet.Listener<\/listener-class>\r\n  <\/listener>\r\n<\/pre>\n<span id=\"WELD-000101_Transactional_services_not_available\"><h3>WELD-000101 Transactional services not available<\/h3><\/span>\n<p>* Error message:<\/p>\n<pre>\r\n232 [main] INFO org.jboss.weld.Bootstrap - WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.\r\n<\/pre>\n<p>* Possible cause: <em>META-INF\/beans.xml<\/em> missing<br \/>\n* Solution: Add <em>META-INF\/beans.xml<\/em> missing to class path<\/p>\n<span id=\"Use_Injected_Beans_in_Constructor\"><h3>Use Injected Beans in Constructor<\/h3><\/span>\n<p>* Don&#8217;t use injected bean instances in constructor since the injected beans have not been initialized hence nulls.<br \/>\n* Use @PostConstruct instead.<\/p>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* The Java EE 6 Tutorial: Basic Concepts, Fourth Edition By: Eric Jendrock et al Chapter 17<br \/>\n* <a href=\"http:\/\/docs.jboss.org\/weld\/reference\/1.0.0\/en-US\/html\/\">Weld &#8211; JSR-299 Reference Implementation<\/a><br \/>\n* <a href=\"http:\/\/www.andygibson.net\/blog\/tutorial\/getting-started-with-jsf-2-0-and-cdi-in-jee-6-part-1\/\">Getting Started with JSF 2.0 and CDI in JEE 6 part 1<\/a><br \/>\n* <a href=\"?p=3849\">CDI: Context and Dependency Injection (JSR-299)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Download WELD * Download Weld from http:\/\/seamframework.org\/Weld\/Downloads * I downloaded weld-1.1.5.Final.zip * Unzip into a local directory, e.g. c:\\prog Example Setup Eclipse Project * Start Eclipse, I used Indigo * Create a new Dynamic Web Project named: test.cdi * Click &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=4373\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[129],"tags":[],"class_list":["post-4373","post","type-post","status-publish","format-standard","hentry","category-cdi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-18x","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4373"}],"version-history":[{"count":15,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4373\/revisions"}],"predecessor-version":[{"id":4379,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4373\/revisions\/4379"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}