lombok and guice injection

荒凉一梦 提交于 2020-01-02 09:52:48

问题


I'm new to lombok and guice injection, I could get the general concept but I ran into some code which I don't understand and can't search due to the syntax. Following is the code, can someone help me understand this?

import com.google.inject.Inject;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__({ @Inject }))
public class SomeClass {
...
}

Thanks!


回答1:


This is going to add a constructor with all fields as parameters, with @Inject annotation and private modifier, so your code will be expanded to:

import com.google.inject.Inject;

public class SomeClass {

    @Inject
    private SomeClass() {
    }
}

This is assuming there is no fields in the class. If you have some fields, then they will be added to the constructor, for example

import com.google.inject.Inject;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__({ @Inject }))
public class SomeClass {
    private String name;
}

Will become

import com.google.inject.Inject;

public class SomeClass {
    private String name        

    @Inject
    private SomeClass(String name) {
        this.name = name;
    }
}

Please note, that this won't work in Guice anyway, as it requires a constructor that is not private, per this documenation: https://github.com/google/guice/wiki/InjectionPoints.

Hope it helps!



来源:https://stackoverflow.com/questions/42910148/lombok-and-guice-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!