Utility Components
Registering optional components
VRaptor has some optional components, inside package br.com.caelum.vraptor.util.
For registering them you can add their packages on web.xml:
<context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>
br.com.caelum.vraptor.util.one.package,
br.com.caelum.vraptor.util.other.package
</param-value>
</context-param>
Or you can create a custom provider:
- 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);
}
}
Available optional components
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. VRaptor also has an interceptor that creates a session
and begins a transaction on the beginning of the request, and closes (and commits or rollbacks)
them on the end of the request. You can register these components by adding the package
br.com.caelum.vraptor.util.hibernate on your web.xml configuration:
<context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>
br.com.caelum.vraptor.util.other.packages...,
br.com.caelum.vraptor.util.hibernate
</param-value>
</context-param>
or register them manually on your custom provider:
@Override
protected void registerCustomComponents(ComponentRegistry registry) {
registry.register(SessionCreator.class, SessionCreator.class); //creates Session's
registry.register(SessionFactoryCreator.class,
SessionFactoryCreator.class); //creates a SessionFactory
registry.register(HibernateTransactionInterceptor.class,
HibernateTransactionInterceptor.class); //open session and transaction in view
}
There is already a built-in 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, by
adding the package br.com.caelum.vraptor.util.jpa on your web.xml:
<context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>
br.com.caelum.vraptor.util.other.packages...,
br.com.caelum.vraptor.util.jpa
</param-value>
</context-param>
or add to your custom provider:
@Override
protected void registerCustomComponents(ComponentRegistry registry) {
registry.register(EntityManagerCreator.class,
EntityManagerCreator.class); //creates EntityManager's
registry.register(EntityManagerFactoryCreator.class,
EntityManagerFactoryCreator.class); //creates an EntityManager
registry.register(JPATransactionInterceptor.class,
JPATransactionInterceptor.class); //open EntityManager and Transaction in view
}
There is already a built-in 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>
Localized Converters
There are some converters for Numbers that are localized, i.e, that consider your current Locale
in order to convert request parameters. You can register them by adding the package
br.com.caelum.vraptor.converter.l10n to your web.xml:
<context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>
br.com.caelum.vraptor.util.other.packages...,
br.com.caelum.vraptor.converter.l10n
</param-value>
</context-param>
Immutable Parameters Instantiator (beta)
If you want to work with immutable objects in your project, you can use a parameter provider that
is able to populate your objects via constructor parameters:
@Resource
public class CarsController {
public void wash(Car car) {
}
}
public class Car {
private final String color;
private final String model;
public Car(String color, String model) {
this.color = color;
this.model = model;
}
//getters
}
The car will be populated with the usual request parameters: car.color and car.model.
To enable this behavior, one can add the package br.com.caelum.vraptor.http.iogi to
its web.xml:
<context-param>
<param-name>br.com.caelum.vraptor.packages</param-name>
<param-value>
br.com.caelum.vraptor.util.other.packages...,
br.com.caelum.vraptor.http.iogi
</param-value>
</context-param>