在Liberty Profile下的OSGI中的Web Bundle中对Mongo db使用@Resource注入 [英] Using @Resource injection for Mongo db, inside Web Bundle inside OSGI, under Liberty Profile

查看:82
本文介绍了在Liberty Profile下的OSGI中的Web Bundle中对Mongo db使用@Resource注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用@Resource将Mongo数据库(com.mongodb.DB)注入OSGI Web Bundle Project.

I want to inject Mongo database (com.mongodb.DB) into OSGI Web Bundle Project, using @Resource.

该应用程序将部署到WebSphere Liberty Profile V8.5.5.1.

The application is to deployed to WebSphere Liberty Profile V8.5.5.1.

这有可能做到吗?

我收到一条错误消息 ....由于以下异常而无法获取注释:java.lang.NoClassDefFoundError"

I got an error message "....The annotations could not be obtained because of the exception : java.lang.NoClassDefFoundError"

但是,如果不使用OSGI,我可以使用在WebSphere Liberty Profile上运行的@Resource将Mongo数据库注入WAR.

However, I can inject Mongo database into WAR, using @Resource, running on WebSphere Liberty Profile, if not using OSGI.

下面是查找返回空值.

public static final DB getMongoDB() throws NamingException {
    InitialContext ic = new InitialContext();
    DB lookup = (DB) ic.lookup("java:comp/env/mongo/testdb");
    return lookup;
}

web.xml是

<resource-env-ref>
    <resource-env-ref-name>mongo/testdb</resource-env-ref-name>
    <resource-env-ref-type>com.mongodb.DB</resource-env-ref-type>
</resource-env-ref>

server.xml是

The server.xml is

<!-- Enable features -->
<featureManager>
    <feature>jsp-2.2</feature>
    <feature>localConnector-1.0</feature>
    <feature>jaxrs-1.1</feature>
    <feature>wab-1.0</feature>
    <feature>ejbLite-3.1</feature>
    <feature>jpa-2.0</feature>
    <feature>jdbc-4.0</feature>
    <feature>cdi-1.0</feature>
    <feature>blueprint-1.0</feature>
    <feature>jndi-1.0</feature>
    <feature>servlet-3.0</feature>
    <feature>json-1.0</feature>
    <feature>mongodb-2.0</feature>
</featureManager>
<httpEndpoint host="localhost" httpPort="29080" httpsPort="29443" id="defaultHttpEndpoint"/>

<library id="MongoLib">
    <file name="${server.config.dir}/lib/mongo-java-driver-2.11.4.jar"/>
</library>
<mongo autoConnectRetry="false" id="mongo" libraryRef="MongoLib">
    <ports>27017</ports>
</mongo>
<mongoDB databaseName="test" id="mongo" jndiName="mongo/testdb" mongoRef="mongo">
</mongoDB>

<osgiApplication id="Test.osgi.jndi.app"
    location="Test.osgi.jndi.app.eba" name="Test.osgi.jndi.app">
    <classloader commonLibraryRef="MongoLib"></classloader>
</osgiApplication>

或者,我尝试用下面的注入替换JNDI查找.注入的资源返回null.

Alternatively, I tried to replace the JNDI lookup with below injection. The injected resource returns null.

public class MongoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Resource (name="mongo/testdb")
    protected DB db;
}

这是OSGI清单版本

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test.osgi.jndi.web
Bundle-SymbolicName: Test.osgi.jndi.web
Bundle-Version: 1.0.0.qualifier
Bundle-ClassPath: WEB-INF/classes
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Web-ContextPath: /Test.osgi.jndi.web
Import-Package: com.mongodb,
 javax.el;version="2.0",
 javax.servlet;version="2.5",
 javax.servlet.annotation,
 javax.servlet.http;version="2.5",
 javax.servlet.jsp;version="2.0",
 javax.servlet.jsp.el;version="2.0",
 javax.servlet.jsp.tagext;version="2.0"
Bundle-Blueprint: OSGI-INF/blueprint/*.xml

控制台:

[AUDIT   ] CWWKZ0001I: Application apps started in 0.054 seconds.
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:29080/Test.osgi.jndi.web/
[AUDIT   ] CWWKZ0001I: Application Test.osgi.jndi.app started in 0.561 seconds.
[AUDIT   ] CWWKF0015I: The server has the following interim fixes installed: PI09253.
[AUDIT   ] CWWKF0011I: The server defaultServer is ready to run a smarter planet.
[WARNING ] CWNEN0046W: The com.mongodb.DB type specified on the resource-ref, resource-env-ref, or message-destination-ref with the osgi:service/mongo/testdb name in the Test.osgi.jndi.web module could not be loaded. Compatibility type checking will not be performed for this resource reference.
[WARNING ] CWNEN0049W: Resource annotations on the methods of the com.osgi.jndi.web.MongoServlet class will be ignored. The annotations could not be obtained because of the exception : java.lang.NoClassDefFoundError: javax.naming.NamingException
[WARNING ] CWNEN0047W: Resource annotations on the fields of the com.osgi.jndi.web.MongoServlet class will be ignored. The annotations could not be obtained because of the exception : java.lang.NoClassDefFoundError: javax.naming.NamingException
    at java.lang.J9VMInternals.verifyImpl(Native Method)
    at java.lang.J9VMInternals.verify(J9VMInternals.java:94)
    at java.lang.J9VMInternals.prepare(J9VMInternals.java:516)
    at java.lang.Class.getDeclaredFields(Class.java:603)
    at com.ibm.wsspi.injectionengine.InjectionProcessor.getAllDeclaredFields(InjectionProcessor.java:549)
    at [internal classes]

推荐答案

我遇到了此错误.可能是您定义: 私有MongoServlet mogoServlet; 我不知道为什么,但是您尝试删除此代码并替换为其他代码.也许会没事的. 它对我有用!

I have got this error. May be you define: Private MongoServlet mogoServlet; I don't know why but you try delete this code and replace by some different code. Maybe it will be ok. It worked for me!

这篇关于在Liberty Profile下的OSGI中的Web Bundle中对Mongo db使用@Resource注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆