Results 1 to 4 of 4

Thread: Access service in bean validation method

  1. #1

    Question Access service in bean validation method

    I am new to Spring, so this should be simple, but I searched everywere and still have no solution:

    I want to invoke a service that validates a value inside the validation method of the model bean, but autowire doens't work there.. So I got this service class:

    Code:
    @Service("globalServices")
    public class GlobalServices {
    ....
    @Autowired
    	private ValidaNIBSP validaNIBSP;
    	
    	public String validaNIB (...) {
    		return validaNIBSP.execute(...);
    		
    	}
    ...
    And my model:
    Code:
    public class SeguradoDTO implements Serializable {
    
      ...
            // executed in validation of edicao view state
    	public void validateEdicao(ValidationContext context) {
    		
    		MessageContext messages = context.getMessageContext();
    		messages.clearMessages();		
                     ...
    
            globalServices.validaNIBSP(...); // how to make this possible????
    Autowire globalServices doesn't work, because the bean SeguradoDTO is just de model associated with the form. So is not part of the ApplicationContext?

    Thanks for your help in advance.

  2. #2

    Default

    I found a solution here:

    http://blog.jdevelop.eu/2008/07/06/a...r-application/

    Though I think there must be a better way to do this.
    So my code now is:

    Code:
    public class SeguradoDTO implements Serializable {
    
      ...
      private         String  cod_entidad;
       ..
    
        public void validateEdicao(ValidationContext context) {
    
        	GlobalServices globalServices = (GlobalServices) AppContext.
        									getApplicationContext().getBean("globalServices");	    	
    		
    
         	globalServices.validaCodEntidad(cod_entidad);
        }

  3. #3

    Smile

    Hi,


    COuld you check if yiu have an entry for <context:annotation-config/> in your applicationContext.xml.@autowired will work only if you have the above entry.

    Thanks & Regards
    Kartik

  4. #4

    Default

    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config />

    Yes, my application is using advanced features of the spring framwork. I use annotation a lot. The only place I can't use it is in an object that is the bean associated with the form:
    So my jsp is:
    Code:
    <form:form action="${flowExecutionUrl}"	modelAttribute="segurado" name="edicaoSeguradoForm" id="edicaoSeguradoForm">
    And the model segurado is defined in the webflow:

    Code:
    <set name="flowScope.segurado" value="null" type="pt.mapfre.msgcrm.segurados.model.SeguradoDTO" />
    Anotations won't work inside the SeguradoDTO class.

    Each time the user submit a form, Spring will instaciate an object of type SeguradoDTO. That's my data transfer Object.

Tags for this Thread

Posting Permissions

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