在Wildfly 10中找不到JNDI @Resource

时间:2016-06-29 10:25:50

标签: jndi wildfly-10

我正在尝试通过Wildfly 10上的RestEasy webservice中的@Resource注释访问JNDI Oracle数据源,但返回null。它适用于InitialContext.lookup方法。这是我的java代码:

Default4.aspx.cs

这是我的standalone.xml数据源定义:

@Path("/rest")
public class ServiceTest {
    @Resource(name = "jdbc/OracleDS") DataSource source1;
    @Resource(name = "java:/jdbc/OracleDS") DataSource source2;
    @Resource(lookup = "java:/jdbc/OracleDS") DataSource source3;

    @GET
    @Path("/test")
    @Produces(MediaType.TEXT_PLAIN)
    public String testJNDI () {
        try {
            javax.naming.Context initCtx = new InitialContext();
            DataSource source4 = (DataSource)initCtx.lookup("java:/jdbc/OracleDS");
            initCtx.close();
            return "" + source1 + " / " + source2 + " / " + source3 + " / " + source4;
        } catch (Exception error) {
            e.printStackTrace();
    }
}

这是我的module.xml代码:

<datasource jta="true" jndi-name="java:/jdbc/OracleDS" pool-name="OracleDS" enabled="true" use-ccm="true" statistics-enabled="true">
   <connection-url>
       jdbc:oracle:thin:@192.168.10.20:1521:log
   </connection-url>
   <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
   <driver>oracle</driver>
   <pool>
       <initial-pool-size>1</initial-pool-size>
       <max-pool-size>10</max-pool-size>
   </pool>
   <security>
       <user-name>user</user-name>
       <password>passw</password>
   </security>
   <validation>
       <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
       <background-validation>true</background-validation>
       <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
       <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
   </validation>
   <timeout>
        <blocking-timeout-millis>30000</blocking-timeout-millis>
        <idle-timeout-minutes>15</idle-timeout-minutes>
        <query-timeout>300</query-timeout>
   </timeout>
   <statement>
       <track-statements>nowarn</track-statements>
   </statement>
</datasource>

服务输出是:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.oracle">
    <resources>
        <resource-root path="ojdbc6.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

为什么source4创建资源而其他资源没有?我预计至少source3会像source4一样。 @Resource定义中有什么问题吗?

由于

1 个答案:

答案 0 :(得分:2)

确保您的WEB-INF目录中有beans.xml

<?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>

This will enable CDI in your application.