nKognito
nKognito

Reputation: 6363

Spring MVC controller's constructor

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>

Upvotes: 3

Views: 15044

Answers (2)

himanshu bisht
himanshu bisht

Reputation: 79

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

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298938

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

Upvotes: 13

Related Questions