我在Tomcat v7.0服务器上运行了一个jsf休眠应用程序,我的问题是 该服务永远不会注入处理程序,因此我在TestWELDHandler的getService()。testMethod1()处获得了一个Nullpointer。
也许有人知道为什么? 我正在尝试添加此处描述的所有内容 https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html
所以我的处理程序看起来像
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestWELDHandler {
private static final Logger log = LoggerFactory.getLogger(TestWELDHandler.class);
@Inject
private TestWELDService service;
public TestWELDHandler() {
log.info("TestWELDHandler");
}
/**
* The method annotated with PostConstruct MUST be invoked even if the class does not request any
* resources to be injected.
*/
@PostConstruct
void init() {
log.info("init service null " + (this.service == null));
}
public String testMethod1FromService() {
log.info("testMethod1FromService service null " + (this.service == null));
return getService().testMethod1();
}
public TestWELDService getService() {
return this.service;
}
public void setService(TestWELDService service) {
this.service = service;
}
}
我有一个界面
public interface TestWELDService {
String testMethod1();
}
和实现
import javax.enterprise.inject.Default;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Default
public class TestWELDServiceImpl implements TestWELDService {
private static final Logger log = LoggerFactory.getLogger(TestWELDServiceImpl.class);
public TestWELDServiceImpl() {
log.info("TestWELDServiceImpl");
}
@Override
public String testMethod1() {
log.info("TestWELDServiceImpl.testMethod1");
return "TestWELDServiceImpl.testMethod1";
}
}
我添加了
<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>
到我的/webapp/WEB-INF/web.xml
和
<Resource name="BeanManager" auth="Container"
type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" />
/webapp/META-INF/context.xml中的context元素
我还必须添加的内容
-Dcom.sun.jersey.server.impl.cdi.lookupExtensionInBeanManager=true
摆脱Tomcat的VM参数
avax.naming.NameNotFoundException: Name [CDIExtension] is not bound in this Context. Unable to find [CDIExtension].
用于TestWELDHandler的bean配置如下:
<managed-bean>
<managed-bean-name>testWELDHandler</managed-bean-name>
<managed-bean-class>com.prosol.faces.handler.weldtest.TestWELDHandler</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>