Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <code>BeanPostProcessor</code> interface, which is called by the <code>ApplicationContext</code> for all created beans, so you have the chance to fill the appropriate properties.</p> <p>I created a simple implementation, which does that:</p> <pre><code>import java.lang.reflect.Field; import java.util.List; import net.vidageek.mirror.dsl.Mirror; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; @Component public class LoggerPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { List&lt;Field&gt; fields = new Mirror().on(bean.getClass()).reflectAll().fields(); for (Field field : fields) { if (Logger.class.isAssignableFrom(field.getType()) &amp;&amp; new Mirror().on(field).reflect().annotation(InjectLogger.class) != null) { new Mirror().on(bean).set().field(field).withValue(LoggerFactory.getLogger(bean.getClass())); } } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } } </code></pre> <p>You don't have to do any complex registration step, since the <code>ApplicationContext</code> is capable of recognizing <code>BeanPostProcessor</code> instances and automatically register them.</p> <p>The <code>@InjectLogger</code> annotation is:</p> <pre><code>import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface InjectLogger { } </code></pre> <p>And then you can easily use the annotation:</p> <pre><code>public static @InjectLogger Logger LOGGER; ... LOGGER.info("Testing message"); </code></pre> <p>I used the <a href="http://projetos.vidageek.net/mirror/mirror/" rel="noreferrer">Mirror</a> library to find the annotated fields, but obviously you may perform a manual lookup in order to avoid this additional dependency.</p> <p>It's actually a nice idea to avoid repeated code, and even small issues that come from copying and paste the <code>Logger</code> definitions from other classes, like when we forget to change the <code>class</code> parameter, which leads to wrong logs.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload