- Jakarta EE Cookbook
- Elder Moraes
- 390字
- 2021-06-24 16:12:43
How it works...
Well, there's a lot happening here! First, we should have a look at the @Produces annotation. This is a Jakarta CDI annotation that says to the server, Hey! This method knows how to construct a User object.
Since 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 @Profile annotation, which has our ProfileType enumeration as a parameter. It is the qualifier for 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 Jakarta 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 these 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, Jakarta CDI is helping us use the context to inject the right implementation of the UserProfile interface.
By taking a look at the endpoint methods, we will see the following:
@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 Jakarta CDI to give us a proper reference to them.
Finally, we have our user event engine, as follows:
@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 Jakarta CDI, Hey CDI! Watch over the User object... when somebody fires an event over it, I want you to do something.
And for something, Jakarta CDI understands this as calling the sendUserNotification and sendUserNotificationAsync methods. Try it for yourself!
Obviously, @Observers will be executed synchronously, while @ObservesAsync will be executed asynchronously.
- 一步一步學Spring Boot 2:微服務項目實戰
- JavaScript修煉之道
- SpringMVC+MyBatis快速開發與項目實戰
- 架構不再難(全5冊)
- vSphere High Performance Cookbook
- 假如C語言是我發明的:講給孩子聽的大師編程課
- HTML5 and CSS3 Transition,Transformation,and Animation
- Visual Basic 6.0程序設計實驗教程
- Hands-On Nuxt.js Web Development
- Web Developer's Reference Guide
- Visual C++從入門到精通(第2版)
- 快樂編程:青少年思維訓練
- Java高手是怎樣煉成的:原理、方法與實踐
- Spring Boot從入門到實戰
- 前端架構設計