Rit*_*ita 5 interceptor cdi jakarta-ee
我有两个不同的项目:A 和 B。
B 包含一个拦截器,我想在项目 A 以及将来的项目 C 和 D 中使用它。
我在两个项目中都使用 jboss-javaee-6.0 版本 3.0.3.Final (这意味着 CDI 版本 1.0)。
项目B(不包含beans.xml):
@InterceptorBinding
@Target({TYPE, METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PerformanceLog {
}
@Interceptor
@PerformanceLog
public class LoggingInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class);
@AroundInvoke
public Object logMethodEntry(InvocationContext ctx) throws Exception {
LOG.debug("Entered method (" + ctx.getMethod().getName() + ") of class (" + ctx.getMethod().getClass() + ").");
}
}
Run Code Online (Sandbox Code Playgroud)
项目A(包含beans.xml):
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">
<interceptors>
<class>commons.utils.logging.performanceInterceptor.LoggingInterceptor</class>
</interceptors>
</beans>
Run Code Online (Sandbox Code Playgroud)
我的控制器C:
@Named
@RequestScoped
public class MyControllerC implements Serializable {
...
@PerformanceLog
public void init() {
//do some BD access here
}
}
Run Code Online (Sandbox Code Playgroud)
当我部署应用程序时,出现错误
weblogic.management.DeploymentException: org.jboss.weld.exceptions.DeploymentException: WELD-001417 Enabled interceptor class <class>commons.utils.logging.performanceInterceptor.LoggingInterceptor </class> in file:/D:/projectos/myProject/target/myProject/WEB-INF/beans.xml@7 is neither annotated @Interceptor nor registered through a portable extension:org.jboss.weld.exceptions.DeploymentException:WELD-001417 Enabled interceptor class <class>commons.utils.logging.performanceInterceptor.LoggingInterceptor</class> in file:/D:/projectos/myProject/target/myProject/WEB-INF/beans.xml@7 is neither annotated @Interceptor nor registered through a portable extension
at org.jboss.weld.bootstrap.Validator.validateEnabledInterceptorClasses(Validator.java:503)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:373)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:379)
at com.oracle.injection.provider.weld.WeldInjectionContainer.start(WeldInjectionContainer.java:110)
at com.oracle.injection.integration.CDIAppDeploymentExtension.initCdi(CDIAppDeploymentExtension.java:76)
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是,因为拦截器位于项目 A 库中的一个 jar 内,所以它是不可见的。我升级到 java-ee 7 版本 1.0.3.Final 和 CDI 1.1,以便我可以在 LoggingInterceptor 中使用标签 @Priority。错误消失了,但是它不起作用(没有任何内容写入日志)。所以我回到了 java-ee 6 版本 3.0.3.Final 和 CDI 1.0。
我猜测也许我找不到拦截器,因为 beans.xml 中的路径不是 jar 中的正确路径(它位于 \WEB-INF\lib\loggings-1.2.jar\commons\utils\logging\performanceInterceptor )。
有人知道可能出了什么问题吗?
您的问题下面的评论为您指明了一个好的方向。
项目 B 应该是所谓的bean archive- 在 Java EE 6 中,它是通过在此类存档中包含一个来实现的beans.xml(甚至完全空的内容就足够了)。
你说:
项目 B 不是一个 Web 应用程序,这就是它没有 beans.xml 的原因。它是一个横向项目,为众多项目提供通用功能
你有点错了——beans.xml在你的情况下仍然必须存在。在 JAR 存档中,应将其放置在src/main/resources/META-INF/文件夹中(如果您使用的是 Maven)。
对于 WAR 包装,应将其放置在src/main/webapp/WEB-INF/.
还有一件事:您在评论中说了两种相反的说法:
项目 B 不是 Web 应用程序
并且:
如果我将 beans.xml 添加到项目 B (...) (/webapp/WEB-INF/beans.xml 因为它是一场战争)
那么真相又是什么呢?
| 归档时间: |
|
| 查看次数: |
9770 次 |
| 最近记录: |