0
我想与Jersy abd Tomcat 7一起工作的CDI拦截器。但它从来没有工作。有人可以帮帮我吗。CDI与泽西岛和Tomcat7 - 拦截器永远不会被调用
我有点尝试由泽西提供的例子进行一些小的修改。
这是我的代码。有趣的是,我可以在输出中看到消息“Injected .....”,意味着@PostConstruct注释正在工作。在WEB-INF
<beans>
<interceptors>
<class>org.glassfish.jersey.examples.cdi.resources.LoggedInterceptor</class>
</interceptors>
<beans/>
Logged.java
的pom.xml
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency> <!-- this is to avoid Jersey jars to be bundled with the WAR -->
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.13</version>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers.glassfish</groupId>
<artifactId>jersey-gf-cdi</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
的beans.xml
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Logged {
}
LoggedInterceptor.java
import java.io.Serializable;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
@Interceptor
@Logged
public class LoggedInterceptor implements Serializable {
public LoggedInterceptor() {
System.out.println("Invoked....");
}
@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();
}
}
LoggedInterceptorTest.java
public class LoggedInterceptorTest {
@Logged
public void testLoggedInterceptor() {
System.out.println("Called...");
}
public static void main(String[] args) {
LoggedInterceptorTest l = new LoggedInterceptorTest();
l.testLoggedInterceptor();
}
}
服务 EchoParamFieldResource.java
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
@ManagedBean
@Path("echofield/{b}")
public class EchoParamFieldResource {
@PathParam("b") String bInjected;
String b;
/**
* Ensure we got path parameter value injected.
*/
@PostConstruct
@SuppressWarnings("unused")
private void postConstruct() {
if (bInjected == null) {
throw new IllegalStateException("Field b has not been injected!");
}
b = bInjected;
System.out.println("Injected.....");
LoggedInterceptorTest l = new LoggedInterceptorTest();
l.testLoggedInterceptor();
}
/**
* Return a string containing injected values.
*
* @param a value of a query parameter a.
* @return message containing injected values.
*/
@GET
@Produces("text/plain")
public String get(@QueryParam("a") String a) {
return String.format("ECHO %s %s", a, b);
}
}
您是否对焊接servlet有依赖性? PostConstruct可以由JAX-RS运行时以及CDI运行时调用。 – 2014-12-05 10:33:08
可能是http://stackoverflow.com/questions/27269482/java-cdi-interceptor-not-working-web-application-with-weld,但是这个问题没有被正确回答。 – 2014-12-05 10:34:22