3

I am trying to put some initialization methods in controller's default constructor, but the problem that it never called. When I put an @Autowired annotation, the error is throuwn - Autowired annotation requires at least on argument.

What the best practice for putting some initialization code in one place except of putting it in each controller's method? Thank you

@InitBinder 
public void initBinder(WebDataBinder binder) { 
   try { 
      initialize(); 
      Logger l = Logger.getLogger(this.getClass().getName()); 
      l.warning("Init!!!"); 
   } catch (Exception e) { 
      e.printStackTrace(); 
   } 
} 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="de.butler.crm.controller" />
    <mvc:annotation-driven />
    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="l" />
    </bean>
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
        <property name="interceptors">
           <list>
            <ref bean="localeChangeInterceptor" />
           </list>
        </property>
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="de.butler.crm.resource.Resources" />
    </bean>
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="de" />
    </bean>
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000"/>
    </bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
1
  • I can't see anything wrong with your XML. Can you post your controller class also? Commented Jan 18, 2012 at 12:24

2 Answers 2

13

a) Controllers are just plain Spring Beans, so that all aspects of the Spring Bean lifecycle apply.

I.e. you can autowire properties or constructor parameters (with annotation support), you can initialize beans using the InitializingBean interface or a @PostConstruct method etc.

If none of this works, then there's something wrong with your setup and you'll have to post your web context xml and / or a stack trace.

b) If you need a per-request setup, then use the @InitBinder mechanism

Sign up to request clarification or add additional context in comments.

8 Comments

And that @ItitBinder method will be executed each request?
By the way, it doesn't help, the controller does not execute that metnod in request
@InitBinder public void initBinder(WebDataBinder binder) { try { initialize(); Logger l = Logger.getLogger(this.getClass().getName()); l.warning("Init!!!"); } catch (Exception e) { try { eventsLogService.add(new LogEvent(e, currentUser, new Date())); } catch (Exception ex) { ex.printStackTrace(); } } }
This construction does not work. Should I add something into configuration file?
@nKognito a) define "does not work". What happens. b) don't put code blocks in comments, add them to your question
|
0

By default life cycle of bean in spring is managed by Spring container .If you want to initialized your bean with some of your code you can do it with @postconstruct method.

You have to create listener for your bean and whenever your bean is initialized this two method will invoke @postconstruct and @predestroy so you have to use this method for doing things

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.