我正在测试/切换到
Java EE7(Glassfish 4),我遇到的一个问题是拦截器,每当我尝试运行项目时,我都会收到以下错误.
SEVERE: Exception while loading the app : CDI deployment
failure:WELD-001417 Enabled interceptor class
com.xxxxxx.security.SecuredInterceptor in
file:/home/xxxxxx/xxxxxx/target/xxxxxx/WEB-INF/beans.xml@7 is neither
annotated @Interceptor nor registered through a portable extension
我正在看CDI 1.1规范的1.3.6节,看起来没有任何改变,所以我做错了什么?
这是我正在使用的代码;
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface Secured {}
@Secured
@Interceptor
public class SecuredInterceptor implements Serializable
{
@AroundInvoke
public Object interceptSecured(InvocationContext ic) throws Exception
{
// Do Stuff
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
<interceptors>
<class>com.xxxxxx.security.SecuredInterceptor</class>
</interceptors>
</beans>
解决方法
来自CDI规范的第12.1节.
A bean archive which contains a beans.xml file with no version has a default bean discovery mode of all.
您的1.1版beans.xml具有bean-discovery-mode =“annotated”.将beans.xml更改为bean-discovery-mode =“all”,我的猜测是它会像从beans.xml中删除版本并使用旧命名空间一样工作,就像在CDI 1.0中一样.