gpt4 book ai didi

java - CDI 拦截器未启动

转载 作者:行者123 更新时间:2023-12-01 18:22:26 24 4
gpt4 key购买 nike

我的拦截器没有在它应该启动的时候启动,即使它已在 beans 中注册并且文件没有提供任何警告。我缺少什么?

编辑 1: 我希望能够在每次调用并离开 Genres.java 中的函数时进行记录,但我根本没有得到任何输出这个配置。

编辑 2:按照 Svetlin 的建议应用断点后,我可以确认代码永远不会到达拦截器或生产者。

beans.xml

<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"
version="1.1" bean-discovery-mode="all">
<interceptors>
<class>no.krystah.log.LoggingInterceptor</class>
</interceptors>
</beans>


LoggingInterceptor.java

package no.krystah.log;

import javax.inject.Inject;
import javax.interceptor.AroundConstruct;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import org.slf4j.Logger;

@Log @Interceptor
public class LoggingInterceptor {

@Inject
Logger logger;

@AroundConstruct
private void init(InvocationContext ic) throws Exception {
logger.info("Entering constructor");
try {
ic.proceed();
} finally {
logger.info("Exiting constructor");
}
}
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
logger.info(ic.getTarget().toString()+" - "+ ic.getMethod().getName());
try {
return ic.proceed();
} finally {
logger.info(ic.getTarget().toString()+ " - "+ ic.getMethod().getName());
}
}
}


Log.java

package no.krystah.log;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
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 Log {
}


LoggerProducer.java

package no.krystah.log;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.bean.SessionScoped;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SessionScoped
public class LoggerProducer {

@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}

流派.java

package no.krystah;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import no.krystah.entity.Genre;
import no.krystah.log.Log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Log
@ManagedBean
@SessionScoped
public class Genres {

public Genres() { }

/* Functions */
}

最佳答案

我认为问题是您正在使用的 @SessionScoped 注释。导入表明您正在使用

import javax.faces.bean.SessionScoped;

请切换到CDI:

import javax.enterprise.context.SessionScoped;

另外,您不应该使用@ManagedBean注释,请通过java ee 7替换它:

@Named

关于java - CDI 拦截器未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27321903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com