龙目岛和Guice注射 [英] lombok and guice injection

查看:112
本文介绍了龙目岛和Guice注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对lombok和guice注入不熟悉,我可以了解一般概念,但是由于语法的原因,我遇到了一些我不理解也无法搜索的代码.以下是代码,有人可以帮助我理解吗?

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 {
...
}

谢谢!

推荐答案

这将添加一个带有所有字段作为参数的构造函数,并带有@Inject注释和private修饰符,因此您的代码将扩展为: /p>

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;
}

将成为

import com.google.inject.Inject;

public class SomeClass {
    private String name        

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

请注意,根据以下文档,这无论如何在Guice中都不起作用,因为它需要一个非私有的构造函数:

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.

希望有帮助!

这篇关于龙目岛和Guice注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆