我在一个在Tomcat应用服务器上使用JSF和MyBatis开发的Web应用程序中使用CDI(Weld)。
当我将一个bean注入MyBatis的插件时,注入的bean总是有一个空值,而不是注入正确的bean实例。
我做错了什么?
问候
import es.metadata.metaqualitas.web.SessionBean;
import java.io.Serializable;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
@Intercepts({
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})
public class ExamplePlugin implements Interceptor, Serializable {
//log has a ApplicationScope
@Inject
private Logger log;
//sessionBean has a SessionScoped
@Inject
private SessionBean sessionBean;
@Override
public Object intercept(Invocation invocation) throws Throwable {
//log and sessionBean are always worth null and launch NullPointerException
log.info(sessionBean.getUsuario());
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
@ApplicationScoped
@Named
public class LoggerProducer {
@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
-------------------------------------------
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class SessionBean implements Serializable {
private String usuario;
public String getUsuario(){
return usuario;
}
public void setUsuario(String usuario){
this.usuario = usuario;
}
@PostConstruct
public void init() {
usuario = "Pedro";
}
}