我不明白为什么没有找到拦截器。我收到以下错误:找不到类,或者没有使用@Interceptor注释,仍然没有通过可移植扩展注册,或者没有在隐式bean存档中使用@Dependent注释 拦截器本身:
@Interceptor
@Loggable
public class LoggingInterceptor {
@Inject
private Logger logger;
@AroundConstruct
public void init(InvocationContext ic) throws Exception {
logger.fine("Entering constructor");
try {
ic.proceed();
} finally {
logger.fine("Exiting constructor");
}
}
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
logger.entering(ic.getTarget().toString(),ic.getMethod().getName());
try {
return ic.proceed();
} finally {
logger.exiting(ic.getTarget().toString(),ic.getMethod().getName());
}
}
}
这是指定的bean.xml:
<?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="all">
<alternatives>
<class>org.abondar.experimental.javaeedemo.basiccdi.test.MockGenerator</class>
</alternatives>
<interceptors>
<class>org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor</class>
</interceptors>
</beans>
日志制作人:
public class LoggingProducer {
@Produces
private Logger createLogger(InjectionPoint injectionPoint){
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
可记录注释:
@InterceptorBinding
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {
}
为了运行应用程序我正在使用Weld Se.Here是Main.class
public class Main {
public static void main(String[] args) {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
BookService service = container.select(BookService.class).get();
Book book = service.createBook("Salo",5.0f,"A book of salo");
System.out.println(book);
weld.shutdown();
}
}
我认为还有另一个奇怪的问题导致我要问的问题:由于基础类加载错误,没有从org.abondar.experimental.javaeedemo.basiccdi.Main生成任何bean定义:键入org.abondar.experimental。 javaeedemo.basiccdi.Main not found。
我看到这个消息与我的例子中的所有类有关。
所以整个堆栈跟踪:
Nov 16, 2017 1:24:42 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 3.0.1 (Final)
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Main because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Main not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.test.MockGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.test.MockGenerator not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.BookService because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.BookService not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.ThirteenDigits because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.ThirteenDigits not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.IsbnGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.IsbnGenerator not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.NumberGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.NumberGenerator not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Book because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Book not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Loggable because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Loggable not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.LoggingProducer because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.LoggingProducer not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.IssnGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.IssnGenerator not found. If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.EightDigits because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.EightDigits not found. If this is unexpected, enable DEBUG logging to see the full error.
[WARNING]
org.jboss.weld.exceptions.DeploymentException: WELD-001417: Enabled interceptor class org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor (<class>org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor</class> in file:/home/abondar/IdeaProjects/JavaEEDemo/BasicCDI/target/classes/META-INF/beans.xml@12) does not match an interceptor bean: the class is not found, or not annotated with @Interceptor and still not registered through a portable extension, or not annotated with @Dependent inside an implicit bean archive
at org.jboss.weld.bootstrap.Validator.validateEnabledInterceptorClasses(Validator.java:661)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:481)
at org.jboss.weld.bootstrap.WeldStartup.validateBeans(WeldStartup.java:480)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:90)
at org.jboss.weld.environment.se.Weld.initialize(Weld.java:790)
at org.abondar.experimental.javaeedemo.basiccdi.Main.main(Main.java:10)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
at java.base/java.lang.Thread.run(Thread.java:844)
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-9,5,org.abondar.experimental.javaeedemo.basiccdi.Main] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-9,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-2,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-11,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-4,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-13,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-6,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-15,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] NOTE: 7 thread(s) did not finish despite being asked to via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=org.abondar.experimental.javaeedemo.basiccdi.Main,maxpri=10]
java.lang.IllegalThreadStateException
at java.base/java.lang.ThreadGroup.destroy(ThreadGroup.java:776)
at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:321)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
答案 0 :(得分:0)
好的,感谢澄清内容。
问题在于,由于您使用的是Weld SE,因此您的beans.xml
可能无法按照您预期的方式使用。具体来说,bean发现在SE模式下的工作方式不同你得到的错误非常清楚 - 你在拦截器类上缺少@Dependent
。
执行您尝试执行的操作的另一种方法是使用CDI中的SeContainerInitializer
:
SeContainer container = SeContainerInitializer.newInstance()
.enableInterceptors(LoggingInterceptor.class)
.selectAlternatives(MockGenerator.class).initialize();
但我相信你确实在Weld中发现了一个错误,因为你正在使用all
,所有的bean都应该被发现。