在 Java 10 上使用 Wildfly 11 Final。
我有 JASPIC 实现,它本身就可以正常工作。我需要将它连接到数据库,以便我可以在public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {...}
. 然而@Resource(mappedName="java:jboss/datasources/someDB") javax.sql.DataSource db;
总是null
为db
。
对于同一 WAR 存档中的任意 Servlet 类,CDI 按预期工作 - 如果我将确切的行放在 Servlet 类中,它会给出一个DataSource
对象。
我通过手动查找来解决这个问题,private static InitialContext ic;
但我需要 CDI 来处理其他全局事务。有没有人遇到过这样的问题?在元素中WEB-INF/beans.xml
有一个bean-discovery-mode="all"
属性。beans
public class SrvAuthModuleImpl implements ServerAuthModule {
@Resource(mappedName="java:jboss/datasources/someDB") javax.sql.DataSource db; //always null
private static InitialContext ic; //workaround via manual lookup
private CallbackHandler handler;
private Class<?>[] supportedMessageTypes = new Class[] {HttpServletRequest.class, HttpServletResponse.class};
@Override
public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler, @SuppressWarnings("rawtypes") java.util.Map options) throws AuthException {
this.handler = handler;
}
@Override
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {
try (
Connection c = getDb().getConnection(); //works
// Connection c = db.getConnection(); //db is null, exception thrown
) {
} catch (SQLException ex) {
}
return AuthStatus.SUCCESS;
}
@Override
public Class<?>[] getSupportedMessageTypes() {return supportedMessageTypes;}
@Override
public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException {return AuthStatus.SEND_SUCCESS;}
@Override
public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException {}
private static javax.sql.DataSource getDb() {
try {
if (ic == null) {
synchronized(TestServerAuthModule.class) {
if (ic == null) {
ic = new javax.naming.InitialContext();
}
}
}
return (javax.sql.DataSource)ic.lookup("java:jboss/datasources/someDB");
} catch (Exception ex) {
return null;
}
}
}