Announcement Announcement Module
Collapse
No announcement yet.
Custom log4J dependecy injection problem (ioc) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom log4J dependecy injection problem (ioc)

    hello
    I am trying to write a custom Log4J by extending AppenderSkeleton class provided by Log4j, but I can't have the dependency injection realizaed in HibernateAppenderDAO class
    My variable logDao is always null ,so i can't persist my message in the database .
    Can someone help me with this dependecy injection issu ? (of course i've realized a component scan ...)
    Is there a way to tell Spring to realize the dependency injection before charging my custom appender ?
    thanks in advance !
    Code:
    @Service 
    public class HibernateAppenderDAO extends AppenderSkeleton{
      
      @Autowired
      @Qualifier ("logDao")
      LogDAO logDao ;
    
       @Override
       protected void append(LoggingEvent event) {
        // Code ...
        
         logDao.insert(ObjectToPersist);  //----> here my logDao is null , the problem come from here but i don't know how to tell spring to realize the dependency injection 
       }
    
    }
    Last edited by 4promachos; Jun 20th, 2012, 04:21 AM.

  • #2
    Please use [ code][/code ] tags when posting code...

    THe appender isn't a spring bean and it will never be as it is constructed and managed outside of the scope of spring and as such dependency injection will not work.

    Comment


    • #3
      so can I have a way to wray the appender so that I can inject my spring bean inside that class? :cool
      I have found this post but i don't understand how to make it in spring?
      http://stackoverflow.com/questions/1...-a-log4j-appen

      Another approach is to dynamically reconfigure Log4j once Spring context is initialized; e.g. write a listener to catch a ContextStartedEvent, obtain all beans of type Appender from the context and add them to Log4j configuration. This will also allow you to create your appenders as beans but avoid singleton mess somewhat.
      In other words how to configure ContextStartedEvent for log4j appender ?
      Last edited by 4promachos; Jun 20th, 2012, 04:39 AM.

      Comment


      • #4
        That isn't going to work because, as mentioned before, the bean isn't under control of Spring. Log4j creates its own instance. The only thing that might work is to use @Configurable and use aspectj compile time weaving.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          That isn't going to work because, as mentioned before, the bean isn't under control of Spring. Log4j creates its own instance. The only thing that might work is to use @Configurable and use aspectj compile time weaving.

          How do you do implement it ?

          Comment


          • #6
            I suggest you read the reference guide especially the section on @Configurable.

            Comment

            Working...
            X