使用CDI向Jetty部署战争 [英] Deploying a war to Jetty with CDI
问题描述
我在IntelliJ中有一个Maven项目,我试图在该项目中将war文件部署到码头容器.这样做的目的是对所述war文件中的某些功能进行快速集成测试.
I have a maven project in IntelliJ where I am trying to deploy a war file to a jetty container. The purpose of this is for a quick integration test of some of the functionality in said war file.
由于Jetty不随CDI或JNDI一起提供,所以我试图添加对这些的支持,但遇到了一些问题.例如,启动时出现以下错误:
Since out of the box Jetty does not come with CDI or JNDI, I am trying to add support for these but running into some issues. For example, I get the following error on startup:
15:30:50 [34mINFO [0;39m o.a.s.c.CdiObjectFactory - [lookup]: Checking for BeanManager under JNDI key java:comp/BeanManager
15:30:50 [39mDEBUG[0;39m o.a.s.c.CdiObjectFactory - [lookup]: BeanManager lookup failed for JNDI key java:comp/BeanManager
我已将pom-servlet罐包含在pom中,并且还将焊听器添加到了web.xml中,但是它仍然无法正常工作.我正在使用Jetty9.有什么想法吗?
I have included the weld-servlet jar in my pom and also added the weld listener to the web.xml, however it still does not work. I am using Jetty 9. Any ideas?
pom.xml
pom.xml
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>2.0.4.Final</version>
</dependency>
web.xml
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
<description>Object factory for the CDI Bean Manager</description>
<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>
推荐答案
以下配置对我来说适用于Jetty 8.x和9.0.x(暂时不支持9.1+)
The following config works for me on Jetty 8.x and 9.0.x (not 9.1+ for the moment)
这是所需的配置:
在Pom.xml中添加依赖项
Add the dependency in Pom.xml
....
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.1.0.Final</version>
</dependency>
....
请注意,我使用的是weld-servlet
依赖项,其中包含所有必需的Weld和CDI类.
note the fact that i'm using weld-servlet
dependency which contains all needed Weld and CDI classes.
在jetty-env.xml
中,您声明JNDI资源
In jetty-env.xml
you declare the JNDI ressources
<!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
中的
中添加侦听器并公开JNDI资源:
in web.xml
you add the listener and expose the JNDI resource :
...
<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>
...
最后,如果您希望能够在servlet中注入bean,则需要让Jetty通过在WEB-INF
目录中创建以下jetty-web.xml
文件来公开其内部类
And eventually if you want to be able to inject bean in servlet you need to ask Jetty to expose some of its inner class by creating the following jetty-web.xml
file in your WEB-INF
directory
<!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>
请不要错过<Item/>
中的-
,这是告诉Jetty一个类不再是内部类并且可以被webapp看到的方式.这样,Weld将能够装饰Jetty内部servlet类,从而在其中添加CDI注入支持.
Dont miss the -
in <Item/>
, it's the way to tell Jetty that a class is no more an inner class and can be seen by the webapp. With that Weld will be able to decorate Jetty inner servlet class to add CDI Injection support in it.
奖金:使用Maven的码头插件
这很容易,您只需将run
配置文件添加到您的pom.xml
中
It's quite easy, you'll only have to add a run
profile to your pom.xml
like this
<profile>
<id>run</id>
<build>
<defaultGoal>clean jetty:run-forked</defaultGoal>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<stopPort>1353</stopPort>
<stopKey>quit</stopKey>
<contextXml>src/main/webapp/WEB-INF/jetty-web.xml</contextXml>
</configuration>
</plugin>
</plugins>
</build>
</profile>
此后,您只需键入mvn -Prun
即可构建您的应用程序,启动Jetty并在其中部署该应用程序.
After that you'll only have to type mvn -Prun
to build your app, launch Jetty and deploy the app in it.
这篇关于使用CDI向Jetty部署战争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!