让 Java EE 6 拦截器正常工作(Weld on GlassFish 3.1)
我正在开发一个项目,我想使用拦截器来记录 ManagedBean 的使用情况。正如我在规范和许多示例中读到的那样,当前的 CDI 实现应该可以实现。该代码主要是从 Glassfish JavaEE 教程等示例或我在网络上找到的其他来源复制的。其中没有特殊的实现。它应该只记录。
我尝试的是以下内容:
LoggedInterceptor.java
package test.interceptors;
import java.io.Serializable;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {
private static final long serialVersionUID = 1L;
public LoggedInterceptor() {
}
@AroundInvoke
public Object logMethodEntry(InvocationContext invocationContext)
throws Exception {
System.out.println(
"Entering method: " + invocationContext.getMethod().getName()
+ " in class "
+ invocationContext.getMethod().getDeclaringClass().getName());
return invocationContext.proceed();
}
}
Logged.java
package test.interceptors;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({
METHOD,
TYPE
})
public @interface Logged {
}
WEB-INF/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>test.interceptors.LoggedInterceptor</class>
</interceptors>
</beans>
MainController.class
// ... All imports
@Named
@SessionScoped
@Logged
public class MainController implements Serializable {
// Code goes here
}
我得到的是来自 WELD 资源加载器的类未找到异常。
[#|2011-04-04T14:03:37.394+0200|SEVERE|glassfish3.1|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=31;_ThreadName=Thread-1;|Exception while loading the app : Error loading class test.interceptors.LoggedInterceptor
org.jboss.weld.resources.spi.ResourceLoadingException: Error loading class test.interceptors.LoggedInterceptor
at org.jboss.weld.resources.DefaultResourceLoader.classForName(DefaultResourceLoader.java:61)
at org.jboss.weld.manager.Enabled$ClassLoader.apply(Enabled.java:67)
at org.jboss.weld.manager.Enabled$ClassLoader.apply(Enabled.java:55)
at com.google.common.collect.Lists$TransformingRandomAccessList.get(Lists.java:435)
at java.util.AbstractList$Itr.next(AbstractList.java:345)
at org.jboss.weld.manager.Enabled.createMetadataMap(Enabled.java:130)
at org.jboss.weld.manager.Enabled.<init>(Enabled.java:100)
at org.jboss.weld.manager.Enabled.of(Enabled.java:82)
at org.jboss.weld.bootstrap.BeanDeployment.<init>(BeanDeployment.java:104)
at org.jboss.weld.bootstrap.WeldBootstrap$DeploymentVisitor.visit(WeldBootstrap.java:185)
at org.jboss.weld.bootstrap.WeldBootstrap$DeploymentVisitor.visit(WeldBootstrap.java:156)
at org.jboss.weld.bootstrap.WeldBootstrap.startContainer(WeldBootstrap.java:287)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:167)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:262)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:460)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1067)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1247)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:465)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:222)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:234)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: test.interceptors.LoggedInterceptor
at com.sun.enterprise.loader.ASURLClassLoader.findClassData(ASURLClassLoader.java:808)
at com.sun.enterprise.loader.ASURLClassLoader.findClass(ASURLClassLoader.java:696)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at org.jboss.weld.resources.DefaultResourceLoader.classForName(DefaultResourceLoader.java:52)
... 42 more
I'm working on a project where I want to use Interceptors to log usage of a ManagedBean. As I read in the specs and in many examples it should be possible with the current CDI implementations. The code is mainly copied from examples like the Glassfish JavaEE Tutorial or other sources I found on the web. There is no special implementation in it. It should only log.
What I try was the following:
LoggedInterceptor.java
package test.interceptors;
import java.io.Serializable;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {
private static final long serialVersionUID = 1L;
public LoggedInterceptor() {
}
@AroundInvoke
public Object logMethodEntry(InvocationContext invocationContext)
throws Exception {
System.out.println(
"Entering method: " + invocationContext.getMethod().getName()
+ " in class "
+ invocationContext.getMethod().getDeclaringClass().getName());
return invocationContext.proceed();
}
}
Logged.java
package test.interceptors;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({
METHOD,
TYPE
})
public @interface Logged {
}
WEB-INF/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>test.interceptors.LoggedInterceptor</class>
</interceptors>
</beans>
MainController.class
// ... All imports
@Named
@SessionScoped
@Logged
public class MainController implements Serializable {
// Code goes here
}
What I get is a class not found Exception from the WELD Resource Loader.
[#|2011-04-04T14:03:37.394+0200|SEVERE|glassfish3.1|javax.enterprise.system.tools.admin.org.glassfish.deployment.admin|_ThreadID=31;_ThreadName=Thread-1;|Exception while loading the app : Error loading class test.interceptors.LoggedInterceptor
org.jboss.weld.resources.spi.ResourceLoadingException: Error loading class test.interceptors.LoggedInterceptor
at org.jboss.weld.resources.DefaultResourceLoader.classForName(DefaultResourceLoader.java:61)
at org.jboss.weld.manager.Enabled$ClassLoader.apply(Enabled.java:67)
at org.jboss.weld.manager.Enabled$ClassLoader.apply(Enabled.java:55)
at com.google.common.collect.Lists$TransformingRandomAccessList.get(Lists.java:435)
at java.util.AbstractList$Itr.next(AbstractList.java:345)
at org.jboss.weld.manager.Enabled.createMetadataMap(Enabled.java:130)
at org.jboss.weld.manager.Enabled.<init>(Enabled.java:100)
at org.jboss.weld.manager.Enabled.of(Enabled.java:82)
at org.jboss.weld.bootstrap.BeanDeployment.<init>(BeanDeployment.java:104)
at org.jboss.weld.bootstrap.WeldBootstrap$DeploymentVisitor.visit(WeldBootstrap.java:185)
at org.jboss.weld.bootstrap.WeldBootstrap$DeploymentVisitor.visit(WeldBootstrap.java:156)
at org.jboss.weld.bootstrap.WeldBootstrap.startContainer(WeldBootstrap.java:287)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:167)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:262)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:460)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1067)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1247)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:465)
at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:222)
at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168)
at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:234)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: test.interceptors.LoggedInterceptor
at com.sun.enterprise.loader.ASURLClassLoader.findClassData(ASURLClassLoader.java:808)
at com.sun.enterprise.loader.ASURLClassLoader.findClass(ASURLClassLoader.java:696)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at org.jboss.weld.resources.DefaultResourceLoader.classForName(DefaultResourceLoader.java:52)
... 42 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好消息(嗯,有点)——您的代码在我的机器(GlassFish 3.1,带有 Netbeans 7.0 RC1)上运行起来就像一个魅力。
这就是我所做的:
添加了简单的 JSF 页面
单击按钮并检查输出;-)
差不多就是这样。看来您的项目设置有问题 - 您可能想从一个新项目重新开始......?
Good news (well, kind of) - your code works like a charm on my machine (GlassFish 3.1, with Netbeans 7.0 RC1).
Here's what I did:
Added trivial JSF page
Clicked button and checked output ;-)
That's pretty much it. Seems like your project setup is the problem - you might want to start again with a fresh project...?