Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.
See all our reports

JRebel and Java EE – Let’s be productive series: CDI Bean Injection

back off I know dependency injection!

We hope you’re excited for the next installment in Let’s be Productive with Java EE series! In this series we’re summarizing the code changes supported by JRebel that were done during the Java EE productivity webinar with Red Hat’s Arun Gupta. This series is showing you how you can give redeploys the chop while still using Java and get all of the benefits of the Java EE ecosystem. This post will focus on CDI Beans, the previous posts covered JPA and Servlets, check them out if you haven’t!

Part 1: Creating new Servlets
Part 2: Creating new JPA entities


In this post, let’s take the sample Java EE application we’ve been working with and add dependency injection with CDI. Remember, thanks to JRebel we’ll inject our dependencies and not have to restart! In this series, we’re using Red Hat’s WildFly 8.2 and JBoss Developer Studio, but the code we’re writing should work with any Java EE compliant application server.

Up to this point in the series, we’ve created a new HttpServlet using the @WebServlet annotation, added a new JPA entity and have hooked it into the servlet and verified that we’re persisting data, and we didn’t have to stop our app server even once thanks to the class reloading power of JRebel!

This post, we’ll add some dependencies to our application and inject them with Java EE’s CDI, Contexts and Dependency Injection, specification JSRs 299 and 346. Before we start writing code, let’s talk about CDI. Remember, EJBs ARE CDI beans, think about them like squares and rectangles. Not all CDI beans are EJBs, but all EJBs are CDI beans. David Blevins has a great post about EJB vs CDI. CDI is a way to proxy objects in your application to help decouple the components in your code, EJBs are a specialized usecase of CDI. Follow along and we’ll use the @Inject annotation to inject the dependency into our base class and then use a JSF page to display the results.

If you’ve been following along with the previous posts and still have your application running, unfortunately it’s time for us to stop the server. As awesome as JRebel is, there are some things even it can’t do. I know it’s disappointing, but we need to add a beans.xml file, if we were to use Java EE 6, and we need to add a few pieces to our web.xml for JSF.

Our CDI beans are all annotation driven, so the beans.xml will be very empty:


and the web.xml needs JSF configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="" xmlns:xsi="" 
xsi:schemaLocation="" version="3.1">
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-name>Faces Servlet</servlet-name>

Both files should go into the WEB-INF directory of the application.

Let’s make sure JRebel’s javaagent is attached to the JVM and we see the JRebel banner in the console of the application server. You should see something like this:

2015-04-29 09:21:31 JRebel:  #############################################################
2015-04-29 09:21:31 JRebel:  
2015-04-29 09:21:31 JRebel:  JRebel Agent 6.1.2 (201504061000)
2015-04-29 09:21:31 JRebel:  (c) Copyright ZeroTurnaround AS, Estonia, Tartu.
2015-04-29 09:21:32 JRebel:

Our application is running at http://localhost:8080/HelloJavaEE7/ and our previous welcome page is intact, as is the HelloServlet that uses our JPA entity. Now that everything’s setup, let’s create a shared Interface for our beans named CDIBean and it contains a single method getMessage:

public interface CDIBean {
  public String getMessage();

Then, let’s define two implementations of the CDIBean interface

public class CDIBeanImpl1 implements CDIBean {
  public String getMessage() {
    return "Bean1";

public class CDIBeanImpl2 implements CDIBean {
  public String getMessage() {
    return "Bean2";

As you can see, we’ve created two implementations of the CDIBean class, they’re both using the @SessionScoped annotation and have @Named to help differentiate the beans when we inject them.

Let’s inject the beans into a CDIBeanTest class

public class CDIBeanTest {
  @Inject @Named("bean1")
  private CDIBean testBean;

  public String getMessage() {
    return testBean.getMessage();

In our test class, we’re using the @Named annotation with the injection to clarify which of the CDIBeans we want at the injection point. We’re also using the @Named annotation for the class, it can now be referred to as testBean in our JSF page.

Here’s a simple JSF page, named cditest.jsf to get started, it pulls the message from the bean and present it to the user:

<f:view xmlns:f=""
 xmlns:h="" >
 <h:outputText value="#{testBean.message}" />

When we go to http://localhost:8080/HelloJavaEE7/cditest.jsf we see the first bean is being injected and we can verify the message is what we expect.

Showing the original implementation of the CDI Bean

Let’s change our @Named annotation to inject the CDIBean2 implementation

@Inject @Named("bean2")

JRebel saw that we changed the annotation, and figured out what classes and configurations needed to be reloaded in the application.

JRebel: Reloading class 'org.sample.CDIBeanTest'.
JRebel: Reloading configuration

And in our browser, we see the JSF page now shows the message from the new implementation

Shows the new CDI Bean being injected correctly

Thanks to the class reloading power of JRebel, we were able to create new CDI beans, change injection annotations, and verify with JSF that everything worked, all without restarting our application server! Java developers on average waste over one full work month per year waiting for redeploys and rebuilds of applications. Take that time back with JRebel and be more productive with Java EE!



If you have any questions you can find me on twitter at @akoblentz or leave a comment below!

  • Antonio Goncalves

    The problem I find with JRebel, is that if your injection point is not resolved, CDI will not

  • Adam Koblentz

    Hi Antonio, what you found may be an accidental side-effect of what we’re doing with the injection points, I’m opening a ticket for the dev team to look into it.

  • Antonio Goncalves

    Let me know if you need some more information/help