Java Reference
In-Depth Information
Then, we need to register our interceptor in
beans.xml
as follows:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
<interceptors>
<class>
com.ensode.cdiintro.interceptor.LoggingInterceptor
</class>
</interceptors>
</beans>
To register our interceptor, we need to set
bean-discovery-mode
to
all
in the
generated
beans.xml
and add the
<interceptor>
tag in
beans.xml
, with one or
more nested
<class>
tags containing the fully qualified names of our interceptors.
The final step before we can use our interceptor binding type is to annotate the class
to be intercepted with our interceptor binding type:
package com.ensode.cdiintro.controller;
import com.ensode.cdiintro.interceptorbinding.
LoggingInterceptorBinding;
import com.ensode.cdiintro.model.Customer;
import com.ensode.cdiintro.model.PremiumCustomer;
import com.ensode.cdiintro.qualifier.Premium;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
@LoggingInterceptorBinding
@Named
@RequestScoped
public class PremiumCustomerController {
private static final Logger logger = Logger.getLogger(
PremiumCustomerController.class.getName());
@Inject
@Premium