Results 1 to 6 of 6

Thread: Custom log4J dependecy injection problem (ioc)

  1. #1
    Join Date
    Mar 2010
    Location
    PARIS
    Posts
    10

    Post 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 at 03:21 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2010
    Location
    PARIS
    Posts
    10

    Default

    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 at 03:39 AM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Mar 2010
    Location
    PARIS
    Posts
    10

    Default

    Quote 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 ?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I suggest you read the reference guide especially the section on @Configurable.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •