java.lang.ClassCastException:无法将java.lang.String的实例分配给字段co.ejb.util.DefaultLogger.logger

时间:2016-11-08 21:32:46

标签: java

部署到启用了可用性的群集时,我们遇到此问题。它可以毫无问题地部署到独立实例。

[2016-11-08T15:25:04.013-0600] [Payara 4.1] [WARNING] [] [org.glassfish.grizzly.http.server.Request] [tid: _ThreadID=28 _ThreadName=http-thread-pool(3)] [timeMillis: 1478640304013] [levelValue: 900] [[
  GRIZZLY0204: Unexpected error during afterService notification
java.lang.ClassCastException: cannot assign instance of java.lang.String to field co.ejb.util.logging.DefaultLogger.logger of type java.util.logging.Logger in instance of co.ejb.util.logging.DefaultLogger
        at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2133)

这是有问题的类文件

package co.ejb.enterprise.interceptor;

import java.io.Serializable;
import java.lang.reflect.Parameter;

import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import co.ejb.util.logging.Logger;

/**
 * A {@link Interceptor} that logs method parameters information in case an exception is thrown from a method
 * in the annotated class. 
 * 
 * CDI lifecycle methods are not intercepted as they need to be targeted by a different type of interceptor and the 
 * lifecycle methods are not called with parameters. 
 */
@Priority(Interceptor.Priority.APPLICATION)
@ExceptionLogging
@Interceptor
public class ExceptionLoggingInterceptor implements Serializable {

    private static final long serialVersionUID = -3886994102592338607L;

    @Inject
    private Logger logger;

    // Methods
    @AroundInvoke
    public Object hanlde(InvocationContext ic) throws Exception {

        try {

            return ic.proceed();
        } catch(Throwable t) {

            StringBuilder message = new StringBuilder("Method name: ").append(ic.getMethod().getName()).append(", method parameters: ");
            int i = 0;
            for (Parameter param : ic.getMethod().getParameters()) {
                message.append("[").append(param.getName()).append(" : ").append(ic.getParameters()[i]).append("], ");
                i++;
            }
            if (i > 0) {
                message.delete(message.length() - 2, message.length());
            }
            logger.warn(message.toString());

            throw t;
        }
    }
}

这是记录器界面

package co.ejb.util.logging;

import java.io.Serializable;

public interface Logger extends Serializable {
    void info(String message);
    void warn(String message);
    void warn(Throwable ex);
    void warn(String message, Throwable ex);
    void error(String message);
    void error(Throwable ex);
    void error(String message, Throwable ex);
}

制片人

package co.ejb.util.logging;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

public class LoggingProducer {

    @Produces
    public Logger defaultLogger(InjectionPoint ip) {
        return new DefaultLogger(ip.getMember().getDeclaringClass().getName());
    }
}

和默认记录器

package co.ejb.util.logging;

import java.util.logging.Level;

import javax.enterprise.inject.Default;

@Default
@Logging()
public class DefaultLogger implements Logger {

    private static final long serialVersionUID = 1L;

    private final java.util.logging.Logger logger;


    public DefaultLogger(String name) {
        this.logger = java.util.logging.Logger.getLogger(name);
    }


    @Override
    public void info(String message) {
        logger.info(message);
    }

    @Override
    public void warn(String message) {
        logger.warning(message);
    }

    @Override
    public void warn(Throwable ex) {
        logger.log(Level.WARNING, ex.getMessage(), ex);
    }

    @Override 
    public void warn(String message, Throwable ex) {
        logger.log(Level.WARNING, message, ex);
    }

    @Override
    public void error(String message){
        logger.severe(message);
    }

    @Override
    public void error(Throwable ex) {
        logger.log(Level.SEVERE, ex.getMessage(), ex);
    }

    @Override
    public void error(String message, Throwable ex) {
        logger.log(Level.SEVERE, message, ex);
    }
}

0 个答案:

没有答案