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.session;
17  
18  import jakarta.inject.Inject;
19  import jakarta.inject.Provider;
20  import jakarta.inject.Singleton;
21  
22  import org.apache.ibatis.session.Configuration;
23  import org.apache.ibatis.session.SqlSessionFactory;
24  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25  
26  /**
27   * Builds the SqlSessionFactory ant let google-guice injects his components.
28   */
29  @Singleton
30  public final class SqlSessionFactoryProvider implements Provider<SqlSessionFactory> {
31  
32    /**
33     * The SqlSessionFactory reference.
34     */
35    private SqlSessionFactory sqlSessionFactory;
36  
37    /**
38     * @since 1.0.1
39     */
40    public SqlSessionFactoryProvider() {
41      // do nothing
42    }
43  
44    /**
45     * Creates a new SqlSessionFactory from the specified configuration.
46     *
47     * @param configuration
48     *          the specified configration.
49     */
50    @Deprecated
51    public SqlSessionFactoryProvider(final Configuration configuration) {
52      // do nothing
53    }
54  
55    /**
56     * Creates a new SqlSessionFactory from the specified configuration.
57     *
58     * @param configuration
59     *          the specified configuration.
60     *
61     * @since 1.0.1
62     */
63    @Inject
64    public void createNewSqlSessionFactory(final Configuration configuration) {
65      this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
66    }
67  
68    /**
69     * {@inheritDoc}
70     */
71    @Override
72    public SqlSessionFactory get() {
73      return sqlSessionFactory;
74    }
75  
76  }