{"id":63034,"date":"2024-08-20T19:39:53","date_gmt":"2024-08-20T14:09:53","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=63034"},"modified":"2024-08-21T17:44:23","modified_gmt":"2024-08-21T12:14:23","slug":"introduction-to-micronaut-with-java","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-micronaut-with-java\/","title":{"rendered":"Introduction to Micronaut with Java"},"content":{"rendered":"<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">What is Micronaut?<\/strong><\/p>\n<p class=\"graf graf--p\">The concept of the Micronaut Foundation stems from the real-time exchange of quality information in the field of building physics, from monoliths to microservices, using the perspective of Spring, Spring Boot, and Grails. With the help of the Micronaut Foundation, the Essential team built and maintained the Micronaut project.<\/p>\n<ul class=\"postList\">\n<li class=\"graf graf--li\">It is created by the OCI team, headed by Graeme Roecher<\/li>\n<li class=\"graf graf--li\">1.0 released in 2018<\/li>\n<li class=\"graf graf--li\">Open-sourced in May 2018<\/li>\n<li class=\"graf graf--li\">Inspired other frameworks to improve and new frameworks to be born on the same approach<\/li>\n<li class=\"graf graf--li\">Latest release 4.5.3<\/li>\n<\/ul>\n<p class=\"graf graf--p\">The Micronaut organization aims to demonstrate all specific practices in JVM design:<\/p>\n<ul class=\"postList\">\n<li class=\"graf graf--li\">Dependency Injection and Inversion of Control (IoC)<\/li>\n<li class=\"graf graf--li\">Aspect Oriented Programming (AOP)<\/li>\n<li class=\"graf graf--li\">Sensible Defaults and Auto-Configuration<\/li>\n<\/ul>\n<p class=\"graf graf--p\">The following resources are provided by the Micronaut Foundation to address the shortcomings of the Spring, and Grails foundations.<\/p>\n<ul class=\"postList\">\n<li class=\"graf graf--li\">Fast startup period<\/li>\n<li class=\"graf graf--li\">Reduced thought footmark<\/li>\n<li class=\"graf graf--li\">Minimal use of impression<\/li>\n<li class=\"graf graf--li\">No runtime bytecode creation<\/li>\n<li class=\"graf graf--li\">Easy Unit Testing<\/li>\n<\/ul>\n<p class=\"graf graf--p\">This aim is realized through the use of Java\u2019s <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/17\/docs\/api\/java.compiler\/javax\/annotation\/processing\/Processor.html\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/17\/docs\/api\/java.compiler\/javax\/annotation\/processing\/Processor.html\"><strong class=\"markup--strong markup--p-strong\">annotation processors<\/strong><\/a>, that are working on some JVM style that supports bureaucracy, in addition to an HTTP Server (accompanying various runtimes <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/micronaut-projects.github.io\/micronaut-servlet\/latest\/guide\/#undertow\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/micronaut-projects.github.io\/micronaut-servlet\/latest\/guide\/#undertow\"><strong class=\"markup--strong markup--p-strong\">Undertow<\/strong><\/a><strong class=\"markup--strong markup--p-strong\">, <\/strong><a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/micronaut-projects.github.io\/micronaut-servlet\/latest\/guide\/#tomcat\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/micronaut-projects.github.io\/micronaut-servlet\/latest\/guide\/#tomcat\"><strong class=\"markup--strong markup--p-strong\">Tomcat<\/strong><\/a><strong class=\"markup--strong markup--p-strong\">, <\/strong><a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/micronaut-projects.github.io\/micronaut-servlet\/latest\/guide\/#jetty\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/micronaut-projects.github.io\/micronaut-servlet\/latest\/guide\/#jetty\"><strong class=\"markup--strong markup--p-strong\">Jetty<\/strong><\/a>, <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/docs.micronaut.io\/4.5.3\/guide\/#nettyHttpClient\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/docs.micronaut.io\/4.5.3\/guide\/#nettyHttpClient\"><strong class=\"markup--strong markup--p-strong\">Netty<\/strong><\/a><strong class=\"markup--strong markup--p-strong\">\u2026<\/strong>\u200b)and an HTTP client (with multiple runtimes <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/docs.micronaut.io\/4.5.3\/guide\/#jdkHttpClient\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/docs.micronaut.io\/4.5.3\/guide\/#jdkHttpClient\"><strong class=\"markup--strong markup--p-strong\">Java HTTP Client<\/strong><\/a>, <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/docs.micronaut.io\/4.5.3\/guide\/#nettyHttpClient\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/docs.micronaut.io\/4.5.3\/guide\/#nettyHttpClient\"><strong class=\"markup--strong markup--p-strong\">Netty<\/strong><\/a><strong class=\"markup--strong markup--p-strong\">,\u00a0\u2026\u200b<\/strong>).<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Key Features of Micronaut<\/strong><\/p>\n<ol class=\"postList\">\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Compile-Time Dependency Injection (DI):<\/strong> Micronaut performs dependency injection at compile time, unlike traditional Java programs that use runtime images. This approach eliminates the performance overhead associated with images, resulting in faster start-up times and reduced memory consumption.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Ahead of Time (AOT) Compilation:<\/strong> Micronaut uses AOT compilation to pre-calculate most application configurations and wiring. This results in efficient and high-quality applications that are ready to run as soon as they are deployed.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Functional applications:<\/strong> Micronaut\u2019s responsive operating system provides robust support, enabling users to create scalable concurrent applications. With built-in support for libraries like Reactor and RxJava, Micronaut makes it easy to use responsive modeling in your applications.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Minimal Configuration: <\/strong>With practical defaults and minimum boilerplate code, Micronaut makes it smooth to get started and reduces the overhead related to utility configuration.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Interoperability: <\/strong>Micronaut integrates seamlessly with existing Java libraries and frameworks, ensuring that builders can leverage their existing knowledge and codebase while adopting Micronaut.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Simplified testing skills: <\/strong>Testing is a crucial issue of software program development and Micronaut stands proud on this subject with its simplified checking out talents. By presenting an integrated guide for unit, integration, and stop-to-end testing, Micronaut empowers developers to write strong tests that ensure the reliability and balance of their applications.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Lightning-Fast Start-up Time: <\/strong>One of the standout functions of Micronauts is its lightning-speedy start-up time. Unlike many different frameworks that may be slow to initialize, Micronaut boasts a remarkable pace, permitting builders to get up and strolling in a count of milliseconds.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Modular Architecture: <\/strong>Micronaut\u2019s modular architecture permits you to select and pick out the functions you need, resulting in a lightweight and green application. This modularity additionally makes it easy to increase the framework with custom modules as needed.<\/li>\n<\/ol>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Benefits of Using Micronaut<\/strong><\/p>\n<ol class=\"postList\">\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Performance<\/strong>: Compile-time DI and AOT compilation methods used by Micronaut provide significant performance improvements. Applications developed with Micronaut launch faster and consume less memory than applications developed with traditional Java frameworks. This is especially beneficial for microservices and serverless applications, where fast start-up times and resource efficiency are important.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Flexibility:<\/strong> Micronaut is a versatile framework suitable for various applications. Whether you are building microservices, server-less tasks, or traditional web applications.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Interoperability:<\/strong> Micronaut is designed to work seamlessly with existing Java libraries and programs.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Cloud-native Design:<\/strong> Micronaut was designed from the start to support cloud-native development. With built-in features for usage discovery, distributed analytics, and cloud deployment, Micronaut simplifies the process of building and deploying microservices in a cloud environment this native cloud supports the developers so that they can concentrate on fixing the problems because they don\u2019t have to worry about infrastructure.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Developer Productivity: <\/strong>Micronaut\u2019s minimalist approach to programming and its use of logical pre-sets makes it easy to get started. Developers can quickly build and deploy applications without complicated configuration files or boilerplate code.<\/li>\n<\/ol>\n<h2 class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Setting your own development environment<\/strong><\/h2>\n<p class=\"graf graf--p\">Before we dive into the Micronaut application, let\u2019s set up our development environment.<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Prerequisites<\/strong><br \/>\nJava 8 or more<br \/>\nGradle or Maven<br \/>\nIDE (Eclipse, IntelliJ IDEA, etc.)\u00a0.<\/p>\n<ol class=\"postList\">\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Install Micronaut Command-Line Interface (CLI):<\/strong> Micronaut offers a CLI tool(you can use <a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/sdkman.io\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/sdkman.io\/\"><strong class=\"markup--strong markup--li-strong\">SDKMAN<\/strong><\/a>) to streamline the process of creating and handling tasks.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Micronaut Launch: <\/strong>if the CLI is not installed, you can create the same application by visiting <a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/launch.micronaut.io\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/launch.micronaut.io\/\"><strong class=\"markup--strong markup--li-strong\">Micronaut Launch<\/strong><\/a>.<\/li>\n<li class=\"graf graf--li\"><strong class=\"markup--strong markup--li-strong\">Setting up an IDE: <\/strong><a class=\"markup--anchor markup--li-anchor\" href=\"https:\/\/docs.micronaut.io\/snapshot\/guide\/#ideSetup\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/docs.micronaut.io\/snapshot\/guide\/#ideSetup\">https:\/\/docs.micronaut.io\/snapshot\/guide\/#ideSetup<\/a><\/li>\n<\/ol>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Basic Annotations<\/strong><\/p>\n<p class=\"graf graf--p\">@Controller- In Micronaut, a class is defined as a controller using the <code class=\"markup--code markup--p-code\">@Controller<\/code> annotation. It routes HTTP requests to the proper channels after processing them.<\/p>\n<p><code><\/code><\/p>\n<pre>@Controller(\"\/first\")\r\npublic class FirstController {\r\n\r\n@Get(\"\/\")\r\npublic String firstService() {\r\nreturn \"First Micronaut Service.\";\r\n}\r\n\r\n}<\/pre>\n<p><code><\/code><\/p>\n<p>@Inject- In Micronaut, the <code class=\"markup--code markup--p-code\">@Inject<\/code> annotation is used to set dependencies in your classes.<\/p>\n<p><code><\/code><\/p>\n<pre>@Controller(\"\/service\")\r\npublic class ServiceController {\r\n\r\n@Inject\r\nprivate MyService myService;\r\n\r\n}<\/pre>\n<p>@Singleton- In Micronaut, using the <code class=\"markup--code markup--p-code\">@Singleton<\/code> annotation indicates that a bean instance should be created and shared.<\/p>\n<p><code><\/code><\/p>\n<pre>@Singleton\r\npublic class MyService {\r\n\/\/ Some service logic\r\n}<\/pre>\n<p class=\"graf graf--p\">@Value- In Micronaut, the <code class=\"markup--code markup--p-code\">@Value<\/code> annotation is used to inject configuration properties.<\/p>\n<pre class=\"graf graf--p\">@Singleton\r\npublic class ConfigService {\r\n\r\n@Value(\"${myapp.message}\")\r\nprivate String message;\r\n\r\npublic String getMessage() {\r\nreturn message;\r\n}\r\n\r\n}<\/pre>\n<p class=\"graf graf--p\">@Requires- In Micronaut, the <code class=\"markup--code markup--p-code\">@Requires<\/code> annotation is used to conditionally enable a bean based on certain conditions, like the presence of a property.<\/p>\n<pre class=\"graf graf--p\">@Singleton\r\n@Requires(property = \"myapp.enabled\", value = \"true\")\r\npublic class MyService {\r\n\/\/ Service logic\r\n}<\/pre>\n<p class=\"graf graf--p\">@Property- In Micronaut, the <code class=\"markup--code markup--p-code\">@Property<\/code> annotation is used to bind configuration properties to a class.<\/p>\n<pre class=\"graf graf--p\">@Singleton\r\n@Property(name = \"myapp.service\")\r\npublic class MyAppService {\r\n\r\nprivate final String name;\r\n\r\npublic MyAppService(@Property(name = \"myapp.service.name\") String name) {\r\nthis.name = name;\r\n}\r\n\r\npublic String getName() {\r\nreturn name;\r\n}\r\n\r\n}<\/pre>\n<p class=\"graf graf--p\">@Client- In Micronaut, the <code class=\"markup--code markup--p-code\">@Client<\/code> annotation is used to create a declarative HTTP client.<span class=\"pre--content\"> \u00a0\u00a0<\/span><\/p>\n<pre class=\"graf graf--p\">@Client(\"\/external\")\r\npublic interface ExternalClient {\r\n\r\n@Get(\"\/data\")\r\nString getData();\r\n\r\n}<\/pre>\n<p class=\"graf graf--p\">@EventListener- In Micronaut, the <code class=\"markup--code markup--p-code\">@EventListener<\/code> annotation is used to mark a method as an event listener.<\/p>\n<pre class=\"graf graf--p\">@Singleton\r\npublic class MyEventListener {\r\n\r\n@EventListener\r\npublic void onEvent(Event event) {\r\n\/\/ event logic\r\n}\r\n\r\n}<\/pre>\n<p class=\"graf graf--p\">@Filter- In Micronaut, the <code class=\"markup--code markup--p-code\">@Filter<\/code> annotation is used to define a filter for HTTP requests.<\/p>\n<pre class=\"graf graf--p\">@Filter(\"\/api\/**\")\r\npublic class CustomFilter implements HttpServerFilter {\r\n\r\n@Override\r\npublic Publisher&lt;HttpResponse&lt;?&gt;&gt; doFilter(HttpRequest&lt;?&gt; request, ServerFilterChain chain) {\r\n\/\/ Filtering logic\r\nreturn chain.proceed(request);\r\n}\r\n\r\n}<\/pre>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">For More Annotations<\/strong>\u200a\u2014\u200a<a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/micronaut-projects.github.io\/micronaut-data\/latest\/guide\/#shared\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/micronaut-projects.github.io\/micronaut-data\/latest\/guide\/#shared\">https:\/\/micronaut-projects.github.io\/micronaut-data\/latest\/guide\/#shared<\/a><\/p>\n<p class=\"graf graf--p\"><strong>Source Code\u200a\u2014<\/strong> <a href=\"https:\/\/micronaut-projects.github.io\/micronaut-data\/latest\/guide\/#shared\">https:\/\/github.com\/Akash1691\/employee-management-service<\/a><\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Conclusion\u00a0<\/strong><\/p>\n<p class=\"graf graf--p\">Micronaut is a significant advancement in the development of the Java framework. Its emphasis on functionality, simplicity, and contemporary application development positions it as a preferred option for developers seeking enhanced power and performance. With Micronaut, you can leverage the full power of Java with the performance gains normally associated with low-level systems.<\/p>\n<p class=\"graf graf--p\">Whether you\u2019re building microservices, server less applications, or traditional web applications, Micronaut provides a robust and efficient platform to build on. Start exploring Micronaut today and get Java application development in the future!<\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">References:<\/strong><\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Micronaut Guide\u200a\u2014\u200a<\/strong><a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/micronaut-projects.github.io\/micronaut-data\/latest\/guide\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/micronaut-projects.github.io\/micronaut-data\/latest\/guide\/\"> https:\/\/redis.io\/docs\/getting-started\/installation\/<\/a><\/p>\n<p class=\"graf graf--p\"><strong class=\"markup--strong markup--p-strong\">Miconaut Webinars\u200a<\/strong>\u2014\u200a<a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.youtube.com\/watch?v=Fn_Yac_NReE&amp;list=PLzR4N8lGQvWiNW17TfJ2tksWFjjKJbbqZ&amp;index=26\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/www.youtube.com\/watch?v=Fn_Yac_NReE&amp;list=PLzR4N8lGQvWiNW17TfJ2tksWFjjKJbbqZ&amp;index=26\">https:\/\/www.youtube.com\/watch?v=Fn_Yac_NReE&amp;list=PLzR4N8lGQvWiNW17TfJ2tksWFjjKJbbqZ&amp;index=26<\/a><\/p>\n<p><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p><code><\/code><\/p>\n<p><code><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Micronaut? The concept of the Micronaut Foundation stems from the real-time exchange of quality information in the field of building physics, from monoliths to microservices, using the perspective of Spring, Spring Boot, and Grails. With the help of the Micronaut Foundation, the Essential team built and maintained the Micronaut project. It is created [&hellip;]<\/p>\n","protected":false},"author":1641,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":26},"categories":[446],"tags":[4844],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/63034"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1641"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=63034"}],"version-history":[{"count":9,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/63034\/revisions"}],"predecessor-version":[{"id":64259,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/63034\/revisions\/64259"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=63034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=63034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=63034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}