# 注入服務提供器

這種注入方式是透過繼承 Provider\<T> 去獲取其實例，其代碼如下:

```java
public interface Provider<T> {
  T get();
}
```

範例如下:

```java
public class DatabaseTransactionLogProvider implements Provider<TransactionLog> {
  private final Connection connection;

  @Inject
  public DatabaseTransactionLogProvider(Connection connection) {
    this.connection = connection;
  }

  public TransactionLog get() {
    DatabaseTransactionLog transactionLog = new DatabaseTransactionLog();
    transactionLog.setConnection(connection);
    return transactionLog;
  }
}
```

完成後，到主類註冊。

```java
@ELDPlugin(
        registry = TesterRegistry.class,
        lifeCycle = TesterLifeCycle.class
)
public class ELDTester extends ELDBukkitPlugin {


    @Override
    protected void bindServices(ServiceCollection serviceCollection) {
        serviceCollection.bindServiceProvider(TransactionLog.class, DatabaseTransactionLogProvider.class);
    }

    @Override
    protected void manageProvider(ManagerProvider provider) {

    }
}
```

## 注入 Provider\<T>

除了直接注入 `TransactionLog` 外, 你也可以直接注入 `Provider<TransactionLog>` 來每次獲取新的實例。

```java
public class LogService {

   @Inject
   private Provider<TransactionLog> logProvider;

   public void log(String message){
      TransactionLog logger = logProvider.get();
      // do something ....
   }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eric2788.gitbook.io/eldependenci/legacy-1.0.8/advanced-tutorial/inject-custom-service/zhu-ru-fu-wu-ti-gong-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
