View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.guice.environment;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Named;
20  import jakarta.inject.Provider;
21  import jakarta.inject.Singleton;
22  
23  import javax.sql.DataSource;
24  
25  import org.apache.ibatis.mapping.Environment;
26  import org.apache.ibatis.transaction.TransactionFactory;
27  
28  /**
29   * Provides the myBatis Environment.
30   */
31  @Singleton
32  public final class EnvironmentProvider implements Provider<Environment> {
33  
34    /**
35     * The environment id.
36     */
37    @Inject
38    @Named("mybatis.environment.id")
39    private String id;
40  
41    @Inject
42    private TransactionFactory transactionFactory;
43  
44    @Inject
45    private DataSource dataSource;
46  
47    public void setId(String id) {
48      this.id = id;
49    }
50  
51    public void setTransactionFactory(TransactionFactory transactionFactory) {
52      this.transactionFactory = transactionFactory;
53    }
54  
55    public void setDataSource(DataSource dataSource) {
56      this.dataSource = dataSource;
57    }
58  
59    /**
60     * {@inheritDoc}
61     */
62    @Override
63    public Environment get() {
64      return new Environment(id, transactionFactory, dataSource);
65    }
66  
67  }