Results 1 to 3 of 3

Thread: Annotated Controller, how do I set properties?

  1. #1
    Join Date
    Jul 2008
    Posts
    9

    Default Annotated Controller, how do I set properties?

    Hi!

    I have a controller bean that is annotated with an (at)Controller annotation. Now I want to set some properties at creation time of that controller.

    My controller is (only the relevant parts):

    Code:
    package foo.web;
    
    (at)Controller
    public class ChangeListController {
    
      private int entriesPerPage = 25;
    
      public void setEntriesPerPage(int value) {
        this.entriesPerPage = value;
      }
    }
    I have tried this definition in the application context:

    Code:
      <bean id="changeListController" class="foo.web.ChangeListController">
      	<property name="entriesPerPage" value="50"/>
      </bean>
    Anyhow the controller still uses the default value (25). I guess it's because two controller instances are created that way.

    So, how can I set the properties in that controller?

    Thanks!

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

    Default

    Anyhow the controller still uses the default value (25). I guess it's because two controller instances are created that way.
    That depends on your further configuration. If you use component scanning then yes you have probably 2 instances of the same Controller. If you don't use component scanning the configuration should 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
    Nov 2008
    Posts
    4

    Default

    You can have your component-scanned Controller @Autowire a bean that is specified, with the necessary properties, in your beans definition XML.

    It might sound like defeating the purpose of component scanning, but if many of your controllers access the same properties, (branding information, messages, and the like) then you can have the same "configuration" bean shared among multiple controllers.

    I use three beans when I use component scanned controllers. One is the controller itself. Another is the configuration bean described above, explicitly coded for in the XML, with its properties set up there. The third is a "Model Attributes" helper bean that supplies methods to provide the values that will be plugged into the model as reference data using @ModelAttribute annotated methods in the controller (the @ModelAttribute annotated methods just call the appropriate method in the "model attributes" helper. The model attributes helper isn't a Controller, and has no Controller annotations (it does @Autowire some helpers on its own, management beans from the middle tier of my application. But it supplies attributes without knowing or caring that they're going to be mapped onto model attributes.)

    The configuration bean is the only one that has to be provided for in the beans definition XML: the Controller itself is component-scanned into being, and its model attribute helper bean is autowired.

    Separating things out this way keeps each of the beans really simple.
    Last edited by AlanCanon; Dec 26th, 2008 at 09:15 PM. Reason: New thought

Posting Permissions

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