In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "sample analysis of SpringBoot in Micronaut", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of SpringBoot in Micronaut".
Introduction
Micronaut is a framework named for faster startup times and is usually the first choice for solutions that use AWS Lambda. It uses AOT compilation to determine what the application needs. The result is that the application has less memory footprint, faster startup time, and no reflection.
The framework provides dependency injection, inversion of control (IOC), and aspect-oriented programming (AOP), which is similar to what Spring provides. With Micronaut, you can create command-line applications, HTTP server applications, and even event-driven applications.
Create a project
To create a project, you can go directly to the startup of Micronaut to create your project. This is similar to how spring provides a way to create projects using https://start.spring.io.
You can also use SDKMan to install command-line utilities. The command line utility serves the same purpose as the startup site. For more details on this, see here.
When creating the project, add the Hibernate-JPA function, because we will create a project with the CRUD function. After building the project, we will look at the various things that Spring developers usually do.
Create a Bean
Usually, in the spring, you will use to create a bean @ bean,@Restcontroller,@service,@repository, etc. Micronaut, we have some comments, which are similar. Let's take a look at them.
Controller -define your controller class for your rest endpoint.
Repository -defines your repository bean.
Singleton -defines a bean with singleton scope.
@ prototype -defines a bean with prototype scope
Requestscope -defines the bean with request scope.
There is no @ serviceor@component annotation, but you can use the above annotation to create a service or component.
In addition, @ infrastructure must define a bean that is critical to the running of the application, which should not be overridden, and an annotation that defines the bean scope of each thread of @ threadlocal.
To inject specific bean,Micronaut supports the same injection mechanism based on constructor, setter, and name, which is similar to that provided by spring. In the case of @ autowire, you will now use the @ inject annotation.
You can also have bean lifecycle methods, conditional bean, bean qualifiers, and so on, which are similar to those provided by spring. You can read more about it at any time in the Micronaut documentation here.
Dependency relationship
Right now, I'm creating a CRUD application that communicates with MySQL. The minimum dependency I need from Micronaut is:
Io.micronaut micronaut-inject compile io.micronaut micronaut-http-server-netty compile io.micronaut.data micronaut-data-hibernate-jpa compile io.micronaut.sql micronaut-jdbc-hikari compile
In addition, I have to add a mysql-connector-java driver to communicate with MySQL.
JPA configuration
To create entities, you can use the usual javax.persistence annotations to create entities, define id, columns, and so on.
@ Entity@Table (name = "Orders") public class Order {@ Id @ GeneratedValue (strategy = GenerationType.AUTO) private Long id
Your data source and hibernation configuration also remain virtually unchanged:
Datasources: default: url: jdbc:mysql://localhost:3306/ORDER username: root password: rootjpa: default: properties: hibernate: hbm2ddl: auto: update show_sql: true dialect: org.hibernate.dialect.MySQL8Dialect
To query your database, we get the implementation of the interface you created by extending interfaces such as CRUDRepository or JPARepository from Micronaut. We also use the @ query annotation to provide JPA query support. This is the code for the JPA repository with sample query methods.
@ Repositorypublic interface OrderRepository extends CrudRepository {@ Query ("select o from Order as o") List getAllOrders ();} rest controller
Rest controllers can be done by creating @ controller annotations and providing your GET,PUT,POST using the mapping @ get,@put,@post annotations respectively. All of these comments come from micronaut-http-client dependencies.
@ Controller ("/ order") public class WebController {private final OrderService orderService; public WebController (OrderService orderService) {this.orderService = orderService;} @ Get ("/ {id}") public HttpResponse getOrder (@ PathVariable ("id") Long id) {Optional mayBeOrder = orderService.getOrder (id); if (mayBeOrder.isPresent ()) {return HttpResponse.created (mayBeOrder.get ()) } return HttpResponse.notFound ();} performance
With the above configuration, the application starts in nearly 2 seconds.
_ _ _ | _ _ | _ _ _ | / _ _ _ | _ _ _ | _ _ | | (_ _ | (_) | (_) | (_ | | | _ | _ | | _ | _ |\ _ _ | _ |\ _ _ / | _ | | _ |\ _ _ |\ _ _ _ |\ _ _ | Micronaut (v2.5.8) 12 INFO com.zaxxer.hikari.HikariDataSource 55 HHH000412 07.769 [main] INFO com.zaxxer.hikari.HikariDataSource-HikariPool-1-Starting...12:55:08.150 [main] INFO com.zaxxer.hikari.HikariDataSource-HikariPool-1-Start completed.12:55:08.157 [main] INFO org.hibernate.Version-HHH000412: Hibernate ORM core version [WORKING] 12 Vera 55 VR 08.248 [main] INFO o.h.annotations.common.Version-HCANN000001: Hibernate Commons Annotations { 5.1.2.Final} 12 5.1.2.Final 55 5.1.2.Final [main] INFO org.hibernate.dialect.Dialect-HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect12:55:09.059 [main] INFO io.micronaut.runtime.Micronaut-Startup completed in 1928ms. Server Running: http://localhost:8080
Now, there's a problem.
When the application starts, the bean is not connected, which can delay the occurrence. When the application receives the first request, the bean connection occurs, so the first serviced request is slightly delayed. The subsequent request was very quick.
Now to really improve startup performance, we can create a Native image. With native images, you can get a startup time of about 90 milliseconds.
Yes, 90 milliseconds with JPA CRUD function.
The above is all the content of the article "sample Analysis of SpringBoot in Micronaut". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.