我有一个要求,其中名为MyAppender的类具有初始化名为“Client”的对象的参数。但是这个“Client”应该在名为“LoggerModule”的类中创建,因为它是注入依赖项的那个。我怎么做? MyAppender如何告诉LoggerModule使用它想要的参数注入实例?这是因为MyAppender是我为logback编写的自定义appender。因为它只能读取connfiguration xml文件并获取这些值。因此他有价值观。但是LoggerModule需要这些值来创建客户端实例。我该怎么做呢?下面是我的MyAppender课程。
public class MyAppender extends CustomAppender{
@Inject
@ClientAnnotation
Client client;
@Inject
@ConnectOptionsAnnotation
ConnectOptions connOpts;
String url;
String clientId;
String topic;
@Override
public void init() {
connOpts.setCleanSession(true);
client.setCallback(this);
client.connect();
client.subscribe(topic);
}
@Override
public void close() {
}
@Override
public void handle(ILoggingEvent event) {
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
}
下面是我应该注入客户端实例的模块类。
public class LoggerModule extends AbstractModule {
MqttClient client;
MqttConnectOptions connOpts;
String url;
String clientId;
String topic;
@Override
protected void configure() {
}
@Provides
@ClientAnnotation
public Client provideClient() {
client = new MqttClient(url, clientId);
return client;
}
@Provides
@ConnectOptionsAnnotation
public ConnectOptions provideConnectOptions() {
connOpts = new ConnectOptions();
return connOpts;
}
}
客户端对象需要MyAppender类保存的url和客户端ID。如何将此传递给模块类并获取注入的对象?我对guice完全不熟悉。请指教。