Monday 25 April 2016

Simple CDI Dependency Injection Example with JSF2.1 and JBoss weld 2.2.6 and CDI 1.2 and Tomcat 8.


Contexts and  Dependency Injection is  new API   introduce by Oracle . CDI is a standard way to provide dependency injection in java application .

CDI (JSR-299): Contexts and Dependency Injection for the Java EE platform was finalized in December 2009. CDI 1.1 is due to be released in the first half of 2013 to coincide with the release of Java EE 7.

JBOSS Weld is a stranded implementation   of CDI API .

We will take simple example with JSF application .

Following  jar file required for JSF-CDI Application
for JSF
javax.el-api-3.0.0.jar
javax.faces-2.2.8-02.jar
javax.faces-api-2.2.jar
javax.interceptor-api-1.2.jar

for CDI
weld-servlet-2.2.6.Final.jar
javax.inject-1.jar
cdi-api-1.2.jar

following file required for this application

index.xhtml
web.xml
beans.xml
context.xml
faces-config.xml
Message.java
MessageImpl.java
MessageBean.java

first of all create Message interface





Message.java
public interface  Message {

public void showMessage();

}

MessageImpl.java

package in.jk.cdi;

public class MessageImpl implements Message {

    @Override
    public void showMessage() {
  
        System.out.println("Welcome in the World of CDI with JSF  and JBOSS Weld and Tomcat 8");
      
    }

}
MessageBean.java

package in.jk.cdi;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named("jk")
@RequestScoped
public class MessageBean{

@Inject Message message;

public void showMessage(){
    System.out.println("Context and Dependency Injection ");
    message.showMessage();
}
 }

index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Title</title>
</h:head>
<h:body>

    <h:form>
<h:commandButton value="Get Messgae  " action="#{jk.showMessage}"></h:commandButton>
</h:form>
</h:body>
</html>

Put the following  context.xml file in META_INF folder of application  

context.xml 

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/tomcat-test">
  <Resource
    name="BeanManager"
    auth="Container"
    type="javax.enterprise.inject.spi.BeanManager"
    factory="org.jboss.weld.resources.ManagerObjectFactory"
    />
</Context>

Put the following three file xml   file in WEB_INF folder of application 

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  bean-discovery-mode="all">
 
</beans>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">
   
    </faces-config>



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>efiling</display-name>

  <welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>

  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
 
  <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
      <!-- This enrty require in web.xml to register CDI Dependency Manager  -->
 <resource-env-ref>
    <!-- Enable Weld CDI, also needs META-INF/context.xml entry -->
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
  </resource-env-ref>



</web-app>





Output  is at Console ,...

Context and Dependency Injection
Welcome in the World of CDI with JSF  and JBOSS Weld and Tomcat 8