Java EE 8 Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

Well, there's a lot happening in the previous section! We should first have a look at the @Produces annotation. It is a CDI annotation that says to the server: "Hey! This method knows how to construct a User object."

As we didn't create a default constructor for the User class, the getUser method from our factory will be injected into our context as one.

The second annotation is our custom annotation @Profile that has our enumeration ProfileType as a parameter. It is the qualifier of our UserProfile objects.

Now, let's have a look at these declarations:

@Profile(ProfileType.ADMIN)
public class ImplAdmin implements UserProfile{
...
}

@Profile(ProfileType.OPERATOR)
@Default
public class ImplOperator implements UserProfile{
...
}

This code will teach CDI how to inject a UserProfile object:

  • If the object is annotated as @Profile(ProfileType.ADMIN), use ImplAdmin
  • If the object is annotated as @Profile(ProfileType.OPERATOR), use ImplOperator
  • If the object is not annotated, use ImplOperator, as it has the @Default annotation

We can see them in action in our endpoint declaration:

    @Inject
@Profile(ProfileType.ADMIN)
private UserProfile userProfileAdmin;

@Inject
@Profile(ProfileType.OPERATOR)
private UserProfile userProfileOperator;

@Inject
private UserProfile userProfileDefault;

So CDI is helping us to use the context to inject the right implementation of our UserProfile interface.

Looking at the endpoint methods, we see this:

    @GET
@Path("getUser")
public Response getUser(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result", user);
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}

Note that we included HttpServletRequest and HttpServletResponse as parameters for our method, but annotated them as @Context. So even though this is not a servlet context (when we have easy access to request and response references), we can ask CDI to give us a proper reference to them.

And finally, we have our user event engine:

    @Inject
private Event<User> userEvent;

...

private ProfileType fireUserEvents(ProfileType type){
userEvent.fire(user);
userEvent.fireAsync(user);
return type;
}

public void sendUserNotification(@Observes User user){
System.out.println("sendUserNotification: " + user);
}

public void sendUserNotificationAsync(@ObservesAsync User user){
System.out.println("sendUserNotificationAsync: " + user);
}

So, we are using the @Observes and @ObserversAsync annotations to say to CDI: "Hey CDI! Watch over User object... when somebody fires an event over it, I want you to do something.

And for "something," CDI understands this as calling the sendUserNotification and sendUserNotificationAsync methods. Try it!

Obviously, @Observers will be executed synchronously and @ObservesAsync asynchronously.