Expert Spring MVC and Web Flow

(Dana P.) #1

has to be injected. The isAutowireByName()method returns true, so a bean definition named
jdbcTemplateneeds to be configured in the Spring container (see Listing 9-18). The init()
method verifies whether a JdbcTemplateinstance has been properly injected.


Listing 9-18.Configuring JdbcTemplate to Be Injected in a Custom Valang Function





Next, as shown in Listing 9-19, we have to register this function with
ValangValidatorFactoryBeanto use it in our constraints.


Listing 9-19.Registering queryForInt Custom Function with Valang


<bean id="validator" class=➥
" org.springmodules.validation.ValangValidatorFactoryBean">



{ userId : 1 == queryForInt('select count(0) from users where userId = ' + ➥
userId) : 'User not found in database' }
]]>




key="queryForInt"
>com.apress.expertspringmvc.validation.SqlQueryForIntFunction



Custom Valang function classes can implement a number of Spring callback interfaces to
get hold of the ApplicationContextor resource loader. Supported interfaces are



  • org.springframework.context.ApplicationContextAware

  • org.springframework.context.ApplicationEventPublisherAware

  • org.springframework.beans.factory.BeanFactoryAware

  • org.springframework.context.MessageSourceAware

  • org.springframework.context.ResourceLoaderAware

  • org.springframework.web.context.ServletContextAware


If any of these interfaces are implemented by a registered custom function, Valang
will inject the related object. If your function implements ApplicationContextAwareor
BeanFactoryAware, consider using autowiring to get hold of collaborating services instead
of dependency lookup.


CHAPTER 9 ■VALIDATION 277
Free download pdf