Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pom.xml:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.blah.api</groupId>
- <artifactId>blahAPI</artifactId>
- <packaging>war</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>blah API</name>
- <properties>
- <junit.version>4.11</junit.version>
- <jersey.version>1.11</jersey.version>
- <jdk.version>1.7</jdk.version> <!-- aka Java 7 -->
- <jetty.version>8.1.9.v20130131</jetty.version>
- <weld.version>1.1.10.Final</weld.version>
- </properties>
- <build>
- <finalName>blah</finalName>
- <plugins>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.3</version>
- </plugin>
- <plugin>
- <inherited>true</inherited>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>${jdk.version}</source>
- <target>${jdk.version}</target>
- <encoding>UTF-8</encoding>
- <showWarnings>true</showWarnings>
- <showDeprecation>true</showDeprecation>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.11</version>
- <dependencies>
- <dependency>
- <groupId>org.apache.maven.surefire</groupId>
- <artifactId>surefire-junit47</artifactId>
- <version>2.12</version>
- </dependency>
- </dependencies>
- <configuration>
- <includes>
- <include>**/*Tests.class</include>
- </includes>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <!-- testing dependencies -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>${junit.version}</version>
- <scope>test</scope>
- </dependency>
- <!-- CDI / dependency injection -->
- <dependency>
- <groupId>org.jboss.weld.servlet</groupId>
- <artifactId>weld-servlet</artifactId>
- <version>${weld.version}</version>
- </dependency>
- <!-- jetty dependency -->
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-servlet</artifactId>
- <version>${jetty.version}</version>
- </dependency>
- </dependencies>
- </project>
- ----
- jetty-context.xml
- <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
- <Configure class="org.eclipse.jetty.webapp.WebAppContext">
- <Set name="serverClasses">
- <Array type="java.lang.String">
- <Item>org.eclipse.jetty.servlet.ServletContextHandler.Decorator</Item>
- </Array>
- </Set>
- </Configure>
- ----
- jetty-env.xml
- <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
- <Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
- <New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
- <Arg>
- <Ref id="webAppCtx" />
- </Arg>
- <Arg>BeanManager</Arg>
- <Arg>
- <New class="javax.naming.Reference">
- <Arg>javax.enterprise.inject.spi.BeanManager</Arg>
- <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
- <Arg/>
- </New>
- </Arg>
- </New>
- </Configure>
- ----
- web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app 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/web-app_3_0.xsd"
- version="3.0">
- <servlet>
- <servlet-name>jerklet</servlet-name>
- <servlet-class>com.blah.api.Jerklet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <listener>
- <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
- </listener>
- <resource-env-ref>
- <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>
- ----
- the actual servlet:
- package com.blah.api;
- import java.io.IOException;
- import javax.inject.Inject;
- import javax.servlet.Servlet;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import com.blah.core.greetings.Greeter;
- public class Jerklet implements Servlet {
- @Inject
- Greeter greeter;
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
- }
- @Override
- public ServletConfig getServletConfig() {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public String getServletInfo() {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public void init(ServletConfig arg0) throws ServletException {
- System.out.println("init init init init init init init init init init init init init init init!!!!!!!!!!!!!!!!!!!!");
- System.out.println("and we say: " + this.greeter.greet());
- }
- @Override
- public void service(ServletRequest arg0, ServletResponse arg1)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- }
- }
- ----
- the interface of the injectee:
- package com.blah.core.greetings;
- public interface Greeter {
- public String greet();
- }
- ----
- and the default implementation that I want wired in:
- package com.blah.core.greetings.impl;
- import javax.annotation.PostConstruct;
- import javax.enterprise.inject.Default;
- import com.blah.core.greetings.Greeter;
- @Default
- public class EnglishGreeter implements Greeter {
- private static final String POLITE_ENGLISH_GREETING = "Hello!!";
- @Override
- public String greet() {
- return POLITE_ENGLISH_GREETING;
- }
- @PostConstruct
- private void init() {
- System.out.println("* * * * * * * * * * ");
- System.out.println("* * * * * * * * * * ");
- System.out.println("* * * * * * * * * * ");
- System.out.println("* * * * * * * * * * ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement