TOMCAT-----CDI Programming Example

Source: Internet
Author: User
Tags jboss

This tutorial mainly describes how to configure contexts and depedency injection in Tomcat.

First, Introduction

Since Tomcat is not a Java EE server, some dependent class libraries must be added manually in order for Tomcat to gain support for CDI. The next major development is the CDI configuration and a simple CDI instance.

Second, the development environment

1.Ubuntu 12.04

2 JDK 1.7.0.09

3. Weld 1.1.10

4 Tomcat 7.0.35

Third, the configuration

Developing CDI programs in the Tomcat environment we have to add some of the dependent class libraries implemented by CDI, which we use weld. The MAVEN dependency configuration we need is as follows:

<dependencies>  <dependency>    <groupId>org.jboss.weld.servlet</groupId>    < artifactid>weld-servlet</artifactid>    <version>1.1.10.Final</version>  </ dependency>  <dependency>    <groupId>javax.servlet</groupId>    <artifactId> javax.servlet-api</artifactid>    <version>3.0.1</version>    <scope>provided</ Scope>  </dependency></dependencies>
    

Create an empty Beans.xml file under the Web-inf folder under the Web project

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://java.sun.com/xml/ns/javaee"   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/beans_1_0.xsd "></beans>
    

Last step we configure CDI Listener in 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"  xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5. xsd "  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>tomcat CDI example</display-name>  <listener>    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>  </listener></web-app>

Iv. Creating a Dependency injection bean

Next, define a bean to be injected into the servlet by dependency.

Interface Definition:

Package Com.byteslounge.cdi.bean;public interface Service {  int doWork (int a, int b);}

Implementation class:

Package Com.byteslounge.cdi.bean;public class Servicebean implements Service {  @Override public  int doWork (int A, int b) {    return a + B;  }}

Note: We do not declare any scope on the defined Servicebean, which means that Servicebean will be created with dependent scope. That is, this servicebean will be with his target class ( Servicebean are injected with the same scope as the class, component.


v. Use a servlet for a simple test

Define a servlet to inject the servicebean we define:

Testservlet.javapackage Com.byteslounge.cdi.servlet;import Java.io.ioexception;import Java.io.PrintWriter;import Javax.inject.inject;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.byteslounge.cdi.bean.Service; @WebServlet (name = "Testservlet",  Urlpatterns = {"/TESTCDI"}) public class Testservlet extends HttpServlet {  private static final long Serialversionuid = 2638127270022516617L;    @Inject  Private service service;    protected void doget (HttpServletRequest request,    httpservletresponse response)    throws Servletexception, IOException {        int a = 2;    int b = 3;        PrintWriter out = Response.getwriter ();    Out.println ("Hello World:" + service.dowork (A, b));    Out.close ();  }}

we use the @inject annotation to inject our bean on the service attribute, indicating that when the container is initialized, the implementation of the service is automatically found ——— Servicebean and injected into the servlet.

Source code Download :configuring-cdi-with-tomcat-example.zip

Original address: Http://www.byteslounge.com/tutorials/configuring-cdi-with-tomcat-example

TOMCAT-----CDI Programming Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.