V|Raptor

documentation

configuration, migration and utilization.

Utility Components

Registering optional components

VRaptor has some optional components, inside package br.com.caelum.vraptor.util. For registering them you can do as follows:
  • Create a child class of your DI Profile (Spring is the default):
    package com.companyname.projectName;

    public class CustomProvider extends SpringProvider {
       
    }
  • Register this class as your DI provider on web.xml:
    <context-param>
        <param-name>br.com.caelum.vraptor.provider</param-name>
        <param-value>com.companyname.projectName.CustomProvider</param-value>
    </context-param>
  • Override the registerCustomComponents method and add your optional components:
    package com.companyname.projectName;

    public class CustomProvider extends SpringProvider {
       
       
    @Override
       
    protected void registerCustomComponents(ComponentRegistry registry) {
           
    registry.register(OptionalComponent.class, OptionalComponent.class);
       
    }
    }

Hibernate Session and SessionFactory

If your components need Hibernate Session and SessionFactory, you will need a ComponentFactory to create them for you. If you use annotated entities, and you have a hibernate.cfg.xml in the root of WEB-INF/classes, you can use VRaptor's built-in ComponentFactory. All you have to do is:
@Override
protected void registerCustomComponents(ComponentRegistry registry) {
   
registry.register(SessionCreator.class, SessionCreator.class);
    registry.register
(SessionFactoryCreator.class, SessionFactoryCreator.class);
}
You can also enable the interceptor that manages Hibernate transactions:
@Override
protected void registerCustomComponents(ComponentRegistry registry) {
   
registry.registry(HibernateTransactionInterceptor.class,
                        HibernateTransactionInterceptor.
class);
}
There is already a Provider that adds these three optional components. You can just register it on your web.xml:
<context-param>
    <param-name>br.com.caelum.vraptor.provider</param-name>
    <param-value>br.com.caelum.vraptor.util.hibernate.HibernateCustomProvider</param-value>
</context-param>

JPA EntityManager e EntityManagerFactory

If you have a persistence.xml with the persistence-unit called "default", you can use VRaptor3 built-in ComponentFactories for EntityManager and EntityManagerFactory:
@Override
protected void registerCustomComponents(ComponentRegistry registry) {
   
registry.register(EntityManagerCreator.class, EntityManagerCreator.class);
    registry.register
(EntityManagerFactoryCreator.class,
                        EntityManagerFactoryCreator.
class);
}
You can also enable the interceptor that manages JPA transactions:
@Override
protected void registerCustomComponents(ComponentRegistry registry) {
   
registry.register(JPATransactionInterceptor.class,
                        JPATransactionInterceptor.
class);
}
There is already a Provider that adds these three optional components. You can just register it on your web.xml:
<context-param>
    <param-name>br.com.caelum.vraptor.provider</param-name>
    <param-value>br.com.caelum.vraptor.util.jpa.JPACustomProvider</param-value>
</context-param>
vraptor logo

Apache 2.0 License - VRaptor ©2009 Caelum - Ensino e Inovação

loki|design