Announcement Announcement Module
Collapse
No announcement yet.
BeanPostProcessor Problem with MVC @Controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BeanPostProcessor Problem with MVC @Controller

    Hi everyone,
    i 've a problem with use of BeanPostProcessor in a @Controller component of my application. I've created a custom annotation (@MayaLog) to automatically instantiate org.apache.log4j.Logger in all bean. It works for all component (@Service, @Component) but don't works on @Controller.


    BeanPostProcessor implementation:

    Code:
    @Component
    public class LoggerPostProcessor implements BeanPostProcessor{
    		
    	@Override
    	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    		return bean;
    	}
    	
    	
    	@Override
    	public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
    		ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {
    			public void doWith( Field field) throws IllegalArgumentException, IllegalAccessException {
    				
    				if (field.isAnnotationPresent(MayaLog.class)) {
    					if(!field.isAccessible()) {
    						ReflectionUtils.makeAccessible(field);
    					}
    					MayaLog logAnnotation = field.getAnnotation(MayaLog.class);
    					Logger log = Logger.getLogger(bean.getClass());
    					field.set(bean, log);
    					//ReflectionUtils.setField(field, bean, log);
    				}
    			}
    		});
    		
    		return bean;
    	}
    
    }
    @MayaLog annotation:

    Code:
    @Retention(RUNTIME)
    @Target({FIELD})
    @Documented
    public @interface MayaLog {
    
    }

    Thanks to everyone!!!

  • #2
    Make sure your BeanPostProcessor is defined in the contexts you want it to operate on. If you define it only in the ContextLoaderListener and your Controllers are in the DispatcherServlet they aren't processed by that BeanPostProcessor (BPPs and BFPPs operate only on the factory/context they are defined in.).

    Comment


    • #3
      I haven't found exactly how to solve the problem, but i did find a workaround, this might be useful for anyone having the same issue.
      well instead of using a combination of BeanPostProcessor and a MetaDataScopeResolver, i decided to use a BeanFactoryPostProcessor that manages both the scope resolution and the bean processing. here'ds the code of the BeanFactoryPostProcessor for anyone interested:

      @Component
      public class OaliaBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

      @SuppressWarnings("unchecked")
      public void postProcessBeanFactory(
      ConfigurableListableBeanFactory beanFactory) throws BeansException {
      for(String beanName : beanFactory.getBeanDefinitionNames())
      {
      //Scope resolution
      BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
      Class beanClass= null;
      try {
      beanClass = Class.forName(beanDefinition.getBeanClassName());
      } catch (ClassNotFoundException e) {
      e.printStackTrace();
      }
      if (Command.class.isAssignableFrom(beanClass))
      beanDefinition.setScope("prototype");

      //Stuff that the BeanPostProcessor go here
      //..........
      //..........

      System.out.println("[Post Processing] " + beanName + " [" + beanDefinition.getScope() + "]");
      }
      }
      }
      Last edited by hasselcloud; Mar 5th, 2012, 01:52 PM.

      Comment


      • #4
        First, thanks to all!!
        I've found solution to problem. Thanks to Marten Deinum reply, i learn that DispatcherServlet (declared on web.xml) create a WebApplicationContext and due to this, the @Controller (i use org.springframework.web.servlet.mvc.support.Contro llerClassNameHandlerMapping for match web request url and controller) is instantiated inside this WebApplicationContext. Solution was to define in @Controller's relative configuration file my custom BeanPostProcessor. I hope, i was clear... thanks!!!!

        Comment


        • #5
          thanks for all..

          Comment

          Working...
          X