Location>code7788 >text

Simple comparison of Google Guice and Noear Solon

Popularity:783 ℃/2024-10-29 17:56:23

1. Introduction

Google Guice is a lightweight dependency injection framework that supports Java 5 or later, Noear Solon is also a lightweight dependency injection framework that supports Java 8 or later.

In this article, we will go through some examples to recognize the difference between Guice and Solon, and interoperability.

2. Adding dependencies

guice

<dependency>
    <groupId></groupId>
    <artifactId>guice</artifactId>
</dependency>

solon

<dependency>
    <groupId></groupId>
    <artifactId>solon</artifactId>
</dependency>

3. Basic usage comparisons

  • (a) Injection

Sample project code for Guice (omitting sources for logger and communicator)

public class Communication {
    @Inject
    private Logger logger;

    @Inject
    private Communicator communicator;

    public boolean sendMessage(String message) {
        return (message);
    }
    
    public static void main(String[] args){
        Injector injector = ();
        
        Communication comms = ()
        ("Software Quality Assurance");
    }
}

Sample project code for Solon (omitting sources for logger and communicator)

@Component
public class Communication {
    @Inject
    private Logger logger;

    @Inject
    private Communicator communicator;

    public boolean sendMessage(String message) {
        return (message);
    }
    
    public static void main(String[] args){
        (, args);
        
        Communication comms = ().getBean();
        ("Software Quality Assurance");
    }
}
  • (b) Reverse control binding

Guice. this module implementation binds Communicator to its default implementation class DefaultCommunicatorImpl and injects instances of Default CommunicatorImpl wherever it is found.

public class BasicModule extends AbstractModule {
    @Override
    protected void configure() {
        bind().to();
    }
}

Solon. by adding component annotations to the DefaultCommunicatorImpl class.

@Component
public class DefaultCommunicatorImpl extneds Communicator{
}
  • (c) by name injection

Guice

@Inject @Named("DefaultCommunicator")
Communicator communicator;


@Override
protected void configure() {
    bind()
      .annotatedWith(("DefaultCommunicator"))
      .to();
}

Solon

@Inject("DefaultCommunicator")
Communicator communicator;


@Component("DefaultCommunicator")
public class DefaultCommunicatorImpl extneds Communicator{
}