maxandersen / jcon-playground

Repo used for Quarkus bot demos at jcon 2024
0 stars 0 forks source link

can spring be used with Quarkus? #28

Open maxandersen opened 1 month ago

maxandersen commented 1 month ago

I heard its the bees knees.

jconqbot[bot] commented 1 month ago

Hey there!!! 😊

Absolutely, Spring can be used with Quarkus!!! 🎉 Quarkus provides an extension called "quarkus-spring-di" which enables you to use Spring's dependency injection within a Quarkus application. This makes it super easy to integrate Spring's powerful features with the lightning-fast startup and memory efficiency of Quarkus!

Here's how you can get started:

  1. Add the Quarkus Spring DI Extension to your project. If you're using Maven, you can add it to your pom.xml:

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-spring-di</artifactId>
    </dependency>
  2. Annotate your beans using Spring's annotations like you normally would (@Component, @Service, @Repository, etc.). Quarkus will handle it just fine!

  3. Run your application! You can use JBang to create and run Quarkus applications super quickly!!! JBang and Quarkus are a match made in heaven for quick, snappy, and efficient Java development!!! 😍

For example, you could create a simple JBang script with Quarkus integration like this:

//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.quarkus:quarkus-bom:2.2.0.Final@pom
//DEPS io.quarkus:quarkus-spring-di

import io.quarkus.runtime.Quarkus;

@org.springframework.stereotype.Component
public class MySpringBean {
    public static void main(String... args) {
        Quarkus.run(args);
    }
}

Then just run it with jbang MySpringBean.java!!!

So, go ahead and combine the best of both worlds! If you have further questions, feel free to ask!!!

/maybe-max

maxandersen commented 1 month ago

But what about Hibernate Panache ?

jconqbot[bot] commented 1 month ago

Hey Max and jconqbot[bot],

Max, thank you for your question about integrating Spring with Quarkus. jconqbot has indeed provided a solid starting point. However, you raised an excellent follow-up question about Hibernate Panache.

Hibernate Panache is an ORM extension for Quarkus that greatly simplifies database interactions by offering an active record pattern and a repository pattern. Integrating Spring with Quarkus including Hibernate Panache is quite possible and often highly beneficial.

  1. Add the Required Dependencies: Alongside quarkus-spring-di, you would need to add dependencies for quarkus-hibernate-orm-panache. Here’s the modified pom.xml:

    <dependencies>
       <dependency>
           <groupId>io.quarkus</groupId>
           <artifactId>quarkus-spring-di</artifactId>
       </dependency>
       <dependency>
           <groupId>io.quarkus</groupId>
           <artifactId>quarkus-hibernate-orm-panache</artifactId>
       </dependency>
    </dependencies>
  2. Define Your Entities and Repositories: Hibernate Panache allows for simple entity and repository definitions. Here’s an example setup:

    import io.quarkus.hibernate.orm.panache.PanacheEntity;
    import javax.persistence.Entity;
    
    @Entity
    public class Book extends PanacheEntity {
       public String title;
       public String author;
    }
  3. Spring Bean Using Panache: You can easily use Panache within your Spring beans that you annotated as usual:

    import org.springframework.stereotype.Service;
    import javax.transaction.Transactional;
    import java.util.List;
    
    @Service
    public class BookService {
       @Transactional
       public void addBook(String title, String author) {
           Book book = new Book();
           book.title = title;
           book.author = author;
           book.persist();
       }
    
       public List<Book> listAllBooks() {
           return Book.listAll();
       }
    }
  4. Running the Application: As with the previous example by jconqbot[bot], you can use Quarkus.run(args) to launch your application. The references to both Spring and Panache will get resolved smoothly.

The combination of Spring’s rich ecosystem with Quarkus’s performance optimizations and Hibernate Panache’s simplified ORM can indeed offer a powerful setup. You’ll enjoy rapid development with efficient resource usage.

If you have any more questions or need further clarifications, feel free to ask!

/maybe-dimitris