Rewrite offers an extension that enables CDI support in your service implementations, such as ConfigurationProvider instances, listeners, and more. This is useful when you need to inject beans, or create CDI @Produces methods for information gathered in a ConfigurationProvider.
-
Include the CDI integration extension in your application’s POM file along with Rewrite itself:
<dependency> <groupId>org.ocpsoft.rewrite</groupId> <artifactId>rewrite-servlet</artifactId> <version>${rewrite.version}</version> </dependency> <dependency> <groupId>org.ocpsoft.rewrite</groupId> <artifactId>rewrite-integration-cdi</artifactId> <version>${rewrite.version}</version> </dependency>
-
Use CDI in your configuration providers or listeners:
package com.example; public class ExampleConfigurationProvider extends HttpConfigurationProvider { @Inject private ExampleBean bean; @Override public int priority() { return 10; } @Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .addRule() .when(new HttpCondition() { public boolean evaluate(HttpServletRewrite event, EvaluationContext context) { // bean.doSomething(); } }) .perform(Log.message("I did something with a CDI bean!")); } }