How to inject a non-serializable class (like java.util.ResourceBundle) with Weld

后端 未结 2 784
太阳男子
太阳男子 2020-12-08 11:12

I want to create a Producer that makes it possible to inject a java.util.ResourceBundle into any class in order to get localized Strings easily. My ResourceBundle-Producer l

相关标签:
2条回答
  • 2020-12-08 11:25

    Well, First of all ResourceBundle is not Serializable. See here. And The message is clear

    cannot produce non-serializable instances for injection into non-transient fields of passivating beans

    passivating beans ??? I Think web.PersonHome is Either a Stateful Session Bean or a @ConversationScoped bean. Am i right ??? If so you should mark your bundle property as transient

    private transient @Inject ResourceBundle bundle;
    
    0 讨论(0)
  • 2020-12-08 11:30

    As per the comments thread in the accepted answer of Arthur. I followed this blog as well as this one to carry out a passivation/activation experiment. The experiment proved MrD comment that the transient property will be NULL upon activation. Thus, to deal with non-serializable member properties of a passivation capable bean (namely, sessionscoped, conversationscoped and stateful session beans), I suggest the following solution:

    private ResourceBundle bundle;
    
    @PostConstruct
    @PostActivate
    public void getResourceBundle() {
        bundle = ResourceBundle.getBundle("/messages", locale );
    }
    

    This solution makes sure that the non-serializable property members are reinitialized every time it enters the READY state.

    A final issue to address

    A final issue to address is the injection of an SLF4j Logger which was non-serializable before slf4j 1.5.3, and I quote:

    As of SLF4J version 1.5.3, logger instances survive serialization. Thus, serialization of the host class no longer requires any special action, even when loggers are declared as instance variables.

    Thus as long as your slf4j dependency is 1.5.3 or later you can safely inject an SLF4j Logger as follows:

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

    Assuming you declared the qualifier:

    @Qualifier
    @Retention(RUNTIME)
    @Target({METHOD, FIELD, PARAMETER, TYPE})
    public @interface LogbackLogger {
    }
    

    Then in a passivation-capable bean, inject as follows:

    @Inject
    @LogbackLogger
    private Logger logger;
    
    0 讨论(0)
提交回复
热议问题