Logging exceptions during bean injection

Posted by Marc W on Stack Overflow See other posts from Stack Overflow or by Marc W
Published on 2010-03-20T17:38:30Z Indexed on 2010/03/20 17:41 UTC
Read the original article Hit count: 371

I think this is a pretty basic question, but after Googling around I can't seem to find the answer.

What I need is a way to log some custom output with log4j during Spring bean construction. I have a factory class called ResponderFactory (being used as an instance factory in Spring) with a factory method that can throw 2 different types of exception.

    public CollectorResponder collectorResponder(String inputQueueName) throws ConfigurationException, BrokerConnectionException {}

Now, normally I could wrap a call to this method in a try-catch block with 2 catch clauses to handle the logging situations for each of the exceptions. However, if I'm using Spring to inject this CollectorResponder, created with the factory, into another class I don't see how this is possible.

<bean id="responderFactory" class="com.package.ResponderFactory">
    <constructor-arg index="0" ref="basicDispatcher" />
    <constructor-arg index="1" value="http://localhost:9000" />
</bean>

<bean id="collectorResponder"
      class="com.package.CollectorResponder"
      factory-bean="responderFactory" factory-method="collectorResponder">
    <constructor-arg value="collector.in" />
</bean>

<bean id="collectorConsumer" class="com.package.CollectorConsumer">
    <constructor-arg ref="collectorResponder" />
</bean>

Again, I want to catch these exceptions when the collectorResponder bean is instantiated. Right now I'm dealing with this is CollectorConsumer when I instantiate using new CollectorResponder(...).

Is there any way I can do this?

© Stack Overflow or respective owner

Related posts about dependency-injection

Related posts about java