Skip to content

๐Ÿ’‰ Lightweight spring-like dependency injector for Java with small memory footprint.

License

Notifications You must be signed in to change notification settings

mbaracz/njector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

njector

License: MIT Build

Njector is an lightweight spring-like dependency injector for Java with small memory footprint.

Installation

To build our project through maven, look at the following steps

$ git clone https://github.com/mbaracz/njector.git
$ cd njector
$ mvn clean install

You also can download this, as a dependency using the following setup.

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.mbaracz</groupId>
    <artifactId>njector</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

Features & usage

  1. Application context initialization
public class Main {
    
    public static void main(String[] args) {
        NjectorApplicationContext ctx = Njector.createApplicationContext(Main.class);
    }
}
  1. @Component - used to create instance of a class for later field, constructor or method injection
@Component
public class UserService {
    // ...
}
  1. @Inject - used to inject previously created instance (note: @Component is required to perform injection)
  • Field injection
@Component
public class OrderService {
    
    @Inject 
    private UserService userService;
}
  • Constructor injection (along with use Lombok @AllArgsConstructor or @RequiredArgsConstructor)
@Component
public class OrderService {

    private final UserService userService;
    
    public OrderService(UserService userService) {
        this.userService = userService;
    }
}

When more than one constructor is present in the class, @Inject annotation on constructor is required.

  • Setter injection
public class OrderService {
    
    private UserService userService;

    @Inject
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
  1. @Qualifier - used to differentiate beans when we have more than one bean of the same type
public interface Database {
    // ...
}

@Component
@Qualifier("flatDatabase")
public class FlatDatabase implements Database {
    // ...
}

@Component
@Qualifier("mysqlDatabase")
public class MySqlDatabase implements Database {
    // ...
}

@Component
private class OrderService {
    
    private final Database database;
    
    public OrderService(@Qualifier("mysqlDatabase") Database database) {
        this.database = database;
    }
}
  1. @Configuration - used to mark class as configuration class, where you can create own bean producers methods
  2. @Bean - used to register and create specified class instance, like @Component but only applicable to producer methods in configuration classes
@Configuration
public class AppConfig {

    @Bean
    public MessageReplacer createMessageReplacer() {
        return new MessageReplacer()
                .setEnableUndefinedVariableException(true)
                .setPrefix("%")
                .setSuffix("%");
    }

    @Bean("someString")
    public String registerSomeString() {
        return "someString";
    }

    @Bean("exampleString")
    public String registerExampleString() {
        return "exampleString";
    }

    @Bean
    public int registerSomeNumber() {
        return 1337;
    }
}

@Component
public class AppModule {
    // ...
    public AppModule(
            MessageReplacer replacer,
            @Qualifier("someString") String someString,
            @Qualifier("exampleString") String exampleString,
            int someNumber
    ) {
        // ...
    }
}

When more than one bean of given type is present, you have to specify their names in @Bean annotation.

To-do list

  • Add prototype scope
  • Add @PreConstruct annotation
  • Add lazy initialization

License

Repository is under MIT License.

About

๐Ÿ’‰ Lightweight spring-like dependency injector for Java with small memory footprint.

Topics

Resources

License

Stars

Watchers

Forks

Languages