- 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.
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- 微服務(wù)與事件驅(qū)動(dòng)架構(gòu)
- Instant Zepto.js
- Python測(cè)試開(kāi)發(fā)入門(mén)與實(shí)踐
- 網(wǎng)頁(yè)設(shè)計(jì)與制作教程(HTML+CSS+JavaScript)(第2版)
- Java游戲服務(wù)器架構(gòu)實(shí)戰(zhàn)
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開(kāi)發(fā)習(xí)題解答與上機(jī)指導(dǎo)
- EPLAN實(shí)戰(zhàn)設(shè)計(jì)
- C語(yǔ)言程序設(shè)計(jì)
- Oracle Exadata專家手冊(cè)
- INSTANT Passbook App Development for iOS How-to
- 深入RabbitMQ
- Programming with CodeIgniterMVC
- Python:Deeper Insights into Machine Learning
- UML2面向?qū)ο蠓治雠c設(shè)計(jì)(第2版)