Advertisement
Guest User

Untitled

a guest
Mar 8th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. pom.xml:
  2.  
  3. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>com.blah.api</groupId>
  7.     <artifactId>blahAPI</artifactId>
  8.     <packaging>war</packaging>
  9.     <version>1.0-SNAPSHOT</version>
  10.     <name>blah API</name>
  11.     <properties>
  12.         <junit.version>4.11</junit.version>
  13.         <jersey.version>1.11</jersey.version>
  14.         <jdk.version>1.7</jdk.version> <!-- aka Java 7 -->
  15.         <jetty.version>8.1.9.v20130131</jetty.version>
  16.         <weld.version>1.1.10.Final</weld.version>
  17.     </properties>
  18.     <build>
  19.         <finalName>blah</finalName>
  20.         <plugins>
  21.             <plugin>
  22.                 <artifactId>maven-war-plugin</artifactId>
  23.                 <version>2.3</version>
  24.             </plugin>
  25.             <plugin>
  26.                 <inherited>true</inherited>
  27.                 <groupId>org.apache.maven.plugins</groupId>
  28.                 <artifactId>maven-compiler-plugin</artifactId>
  29.                 <configuration>
  30.                     <source>${jdk.version}</source>
  31.                     <target>${jdk.version}</target>
  32.                     <encoding>UTF-8</encoding>
  33.                     <showWarnings>true</showWarnings>
  34.                     <showDeprecation>true</showDeprecation>
  35.                 </configuration>
  36.             </plugin>
  37.             <plugin>
  38.                 <groupId>org.apache.maven.plugins</groupId>
  39.                 <artifactId>maven-surefire-plugin</artifactId>
  40.                 <version>2.11</version>
  41.                 <dependencies>
  42.                     <dependency>
  43.                         <groupId>org.apache.maven.surefire</groupId>
  44.                         <artifactId>surefire-junit47</artifactId>
  45.                         <version>2.12</version>
  46.                     </dependency>
  47.                 </dependencies>
  48.                 <configuration>
  49.                     <includes>
  50.                         <include>**/*Tests.class</include>
  51.                     </includes>
  52.                 </configuration>
  53.             </plugin>
  54.         </plugins>
  55.     </build>
  56.     <dependencies>
  57.         <!-- testing dependencies -->
  58.         <dependency>
  59.             <groupId>junit</groupId>
  60.             <artifactId>junit</artifactId>
  61.             <version>${junit.version}</version>
  62.             <scope>test</scope>
  63.         </dependency>
  64.         <!-- CDI / dependency injection -->
  65.         <dependency>
  66.             <groupId>org.jboss.weld.servlet</groupId>
  67.             <artifactId>weld-servlet</artifactId>
  68.             <version>${weld.version}</version>
  69.         </dependency>
  70.         <!-- jetty dependency -->
  71.         <dependency>
  72.             <groupId>org.eclipse.jetty</groupId>
  73.             <artifactId>jetty-servlet</artifactId>
  74.             <version>${jetty.version}</version>
  75.         </dependency>
  76.     </dependencies>
  77. </project>
  78.  
  79. ----
  80.  
  81. jetty-context.xml
  82.  
  83. <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
  84. <Configure class="org.eclipse.jetty.webapp.WebAppContext">
  85.     <Set name="serverClasses">
  86.     <Array type="java.lang.String">
  87.         <Item>org.eclipse.jetty.servlet.ServletContextHandler.Decorator</Item>
  88.     </Array>
  89.     </Set>
  90. </Configure>
  91.  
  92. ----
  93.  
  94. jetty-env.xml
  95.  
  96. <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
  97. <Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
  98.     <New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
  99.         <Arg>
  100.             <Ref id="webAppCtx" />
  101.         </Arg>
  102.         <Arg>BeanManager</Arg>
  103.         <Arg>
  104.             <New class="javax.naming.Reference">
  105.                 <Arg>javax.enterprise.inject.spi.BeanManager</Arg>
  106.                 <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
  107.                 <Arg/>
  108.             </New>
  109.         </Arg>
  110.     </New>
  111. </Configure>
  112.  
  113. ----
  114.  
  115. web.xml
  116.  
  117. <?xml version="1.0" encoding="UTF-8"?>
  118. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  119.     xsi:schemaLocation="
  120.         http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  121.     version="3.0">
  122.  
  123.     <servlet>
  124.         <servlet-name>jerklet</servlet-name>
  125.         <servlet-class>com.blah.api.Jerklet</servlet-class>
  126.         <load-on-startup>1</load-on-startup>
  127.     </servlet>
  128.  
  129.     <listener>
  130.         <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  131.     </listener>
  132.  
  133.     <resource-env-ref>
  134.         <resource-env-ref-name>BeanManager</resource-env-ref-name>
  135.         <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
  136.     </resource-env-ref>
  137.  
  138. </web-app>
  139.  
  140. ----
  141.  
  142. the actual servlet:
  143.  
  144. package com.blah.api;
  145.  
  146. import java.io.IOException;
  147. import javax.inject.Inject;
  148. import javax.servlet.Servlet;
  149. import javax.servlet.ServletConfig;
  150. import javax.servlet.ServletException;
  151. import javax.servlet.ServletRequest;
  152. import javax.servlet.ServletResponse;
  153.  
  154. import com.blah.core.greetings.Greeter;
  155.  
  156. public class Jerklet implements Servlet {
  157.    
  158.     @Inject
  159.     Greeter greeter;
  160.  
  161.     @Override
  162.     public void destroy() {
  163.         // TODO Auto-generated method stub
  164.        
  165.     }
  166.  
  167.     @Override
  168.     public ServletConfig getServletConfig() {
  169.         // TODO Auto-generated method stub
  170.         return null;
  171.     }
  172.  
  173.     @Override
  174.     public String getServletInfo() {
  175.         // TODO Auto-generated method stub
  176.         return null;
  177.     }
  178.  
  179.     @Override
  180.     public void init(ServletConfig arg0) throws ServletException {
  181.  
  182.         System.out.println("init init init init init init init init init init init init init init init!!!!!!!!!!!!!!!!!!!!");
  183.         System.out.println("and we say: " + this.greeter.greet());
  184.        
  185.        
  186.     }
  187.  
  188.     @Override
  189.     public void service(ServletRequest arg0, ServletResponse arg1)
  190.             throws ServletException, IOException {
  191.         // TODO Auto-generated method stub
  192.        
  193.     }
  194.  
  195. }
  196.  
  197. ----
  198.  
  199. the interface of the injectee:
  200.  
  201. package com.blah.core.greetings;
  202.  
  203. public interface Greeter {
  204.     public String greet();
  205. }
  206.  
  207. ----
  208.  
  209. and the default implementation that I want wired in:
  210.  
  211. package com.blah.core.greetings.impl;
  212.  
  213. import javax.annotation.PostConstruct;
  214. import javax.enterprise.inject.Default;
  215.  
  216. import com.blah.core.greetings.Greeter;
  217.  
  218. @Default
  219. public class EnglishGreeter implements Greeter {
  220.  
  221.     private static final String POLITE_ENGLISH_GREETING = "Hello!!";
  222.  
  223.     @Override
  224.     public String greet() {
  225.         return POLITE_ENGLISH_GREETING;
  226.     }
  227.  
  228.     @PostConstruct
  229.     private void init() {
  230.         System.out.println("* * * * * * * * * * ");
  231.         System.out.println("* * * * * * * * * * ");
  232.         System.out.println("* * * * * * * * * * ");
  233.         System.out.println("* * * * * * * * * * ");
  234.     }
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement