|
1 |
| -/** |
2 |
| - * Copyright 2009-2016 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 |
| - * http://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; |
17 |
| - |
18 |
| -import com.google.inject.AbstractModule; |
19 |
| -import com.google.inject.Binder; |
20 |
| -import com.google.inject.Scopes; |
21 |
| -import com.google.inject.matcher.AbstractMatcher; |
22 |
| -import org.apache.ibatis.session.SqlSession; |
23 |
| -import org.apache.ibatis.session.SqlSessionManager; |
24 |
| -import org.mybatis.guice.mappers.MapperProvider; |
25 |
| -import org.mybatis.guice.session.SqlSessionManagerProvider; |
26 |
| -import org.mybatis.guice.transactional.Transactional; |
27 |
| -import org.mybatis.guice.transactional.TransactionalMethodInterceptor; |
28 |
| - |
29 |
| -import java.lang.reflect.Method; |
30 |
| - |
31 |
| -import static com.google.inject.matcher.Matchers.annotatedWith; |
32 |
| -import static com.google.inject.matcher.Matchers.any; |
33 |
| -import static com.google.inject.matcher.Matchers.not; |
34 |
| -import static com.google.inject.name.Names.named; |
35 |
| -import static com.google.inject.util.Providers.guicify; |
36 |
| - |
37 |
| -abstract class AbstractMyBatisModule extends AbstractModule { |
38 |
| - |
39 |
| - protected static final AbstractMatcher<Method> DECLARED_BY_OBJECT = new AbstractMatcher<Method>() { |
40 |
| - @Override |
41 |
| - public boolean matches(Method method) { |
42 |
| - return method.getDeclaringClass() == Object.class; |
43 |
| - } |
44 |
| - }; |
45 |
| - protected static final AbstractMatcher<Method> SYNTHETIC = new AbstractMatcher<Method>() { |
46 |
| - @Override |
47 |
| - public boolean matches(Method method) { |
48 |
| - return method.isSynthetic(); |
49 |
| - } |
50 |
| - }; |
51 |
| - |
52 |
| - private ClassLoader resourcesClassLoader = getDefaultClassLoader(); |
53 |
| - |
54 |
| - private ClassLoader driverClassLoader = getDefaultClassLoader(); |
55 |
| - |
56 |
| - /** |
57 |
| - * {@inheritDoc} |
58 |
| - */ |
59 |
| - @Override |
60 |
| - protected final void configure() { |
61 |
| - try { |
62 |
| - // sql session manager |
63 |
| - bind(SqlSessionManager.class).toProvider(SqlSessionManagerProvider.class).in(Scopes.SINGLETON); |
64 |
| - bind(SqlSession.class).to(SqlSessionManager.class).in(Scopes.SINGLETON); |
65 |
| - |
66 |
| - internalConfigure(); |
67 |
| - |
68 |
| - bindTransactionInterceptors(); |
69 |
| - |
70 |
| - bind(ClassLoader.class) |
71 |
| - .annotatedWith(named("JDBC.driverClassLoader")) |
72 |
| - .toInstance(driverClassLoader); |
73 |
| - } finally { |
74 |
| - resourcesClassLoader = getDefaultClassLoader(); |
75 |
| - driverClassLoader = getDefaultClassLoader(); |
76 |
| - } |
77 |
| - } |
78 |
| - |
79 |
| - /** |
80 |
| - * bind transactional interceptors |
81 |
| - */ |
82 |
| - protected void bindTransactionInterceptors() { |
83 |
| - // transactional interceptor |
84 |
| - TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor(); |
85 |
| - requestInjection(interceptor); |
86 |
| - bindInterceptor(any(), not(SYNTHETIC).and(not(DECLARED_BY_OBJECT)).and(annotatedWith(Transactional.class)), interceptor); |
87 |
| - // Intercept classes annotated with Transactional, but avoid "double" |
88 |
| - // interception when a mathod is also annotated inside an annotated |
89 |
| - // class. |
90 |
| - bindInterceptor(annotatedWith(Transactional.class), not(SYNTHETIC).and(not(DECLARED_BY_OBJECT)).and(not(annotatedWith(Transactional.class))), interceptor); |
91 |
| - } |
92 |
| - |
93 |
| - /** |
94 |
| - * |
95 |
| - * @param mapperType |
96 |
| - */ |
97 |
| - final <T> void bindMapper(Class<T> mapperType) { |
98 |
| - bind(mapperType).toProvider(guicify(new MapperProvider<T>(mapperType))).in(Scopes.SINGLETON); |
99 |
| - } |
100 |
| - |
101 |
| - /** |
102 |
| - * |
103 |
| - * @return |
104 |
| - * @since 3.3 |
105 |
| - */ |
106 |
| - public void useResourceClassLoader(ClassLoader resourceClassLoader) { |
107 |
| - this.resourcesClassLoader = resourceClassLoader; |
108 |
| - } |
109 |
| - |
110 |
| - /** |
111 |
| - * |
112 |
| - * @return |
113 |
| - * @since 3.3 |
114 |
| - */ |
115 |
| - protected final ClassLoader getResourceClassLoader() { |
116 |
| - return resourcesClassLoader; |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * |
121 |
| - * @return |
122 |
| - * @since 3.3 |
123 |
| - */ |
124 |
| - public void useJdbcDriverClassLoader(ClassLoader driverClassLoader) { |
125 |
| - this.driverClassLoader = driverClassLoader; |
126 |
| - } |
127 |
| - |
128 |
| - /** |
129 |
| - * |
130 |
| - * @return |
131 |
| - * @since 3.3 |
132 |
| - */ |
133 |
| - private ClassLoader getDefaultClassLoader() { |
134 |
| - return getClass().getClassLoader(); |
135 |
| - } |
136 |
| - |
137 |
| - /** |
138 |
| - * Configures a {@link Binder} via the exposed methods. |
139 |
| - */ |
140 |
| - abstract void internalConfigure(); |
141 |
| - |
142 |
| - /** |
143 |
| - * |
144 |
| - */ |
145 |
| - protected abstract void initialize(); |
146 |
| - |
147 |
| -} |
| 1 | +/** |
| 2 | + * Copyright 2009-2016 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 | + * http://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; |
| 17 | + |
| 18 | +import com.google.inject.AbstractModule; |
| 19 | +import com.google.inject.Binder; |
| 20 | +import com.google.inject.Scopes; |
| 21 | +import com.google.inject.matcher.AbstractMatcher; |
| 22 | +import org.apache.ibatis.session.SqlSession; |
| 23 | +import org.apache.ibatis.session.SqlSessionManager; |
| 24 | +import org.mybatis.guice.mappers.MapperProvider; |
| 25 | +import org.mybatis.guice.session.SqlSessionManagerProvider; |
| 26 | +import org.mybatis.guice.transactional.Transactional; |
| 27 | +import org.mybatis.guice.transactional.TransactionalMethodInterceptor; |
| 28 | + |
| 29 | +import java.lang.reflect.Method; |
| 30 | + |
| 31 | +import static com.google.inject.matcher.Matchers.annotatedWith; |
| 32 | +import static com.google.inject.matcher.Matchers.any; |
| 33 | +import static com.google.inject.matcher.Matchers.not; |
| 34 | +import static com.google.inject.name.Names.named; |
| 35 | +import static com.google.inject.util.Providers.guicify; |
| 36 | + |
| 37 | +abstract class AbstractMyBatisModule extends AbstractModule { |
| 38 | + |
| 39 | + protected static final AbstractMatcher<Method> DECLARED_BY_OBJECT = new AbstractMatcher<Method>() { |
| 40 | + @Override |
| 41 | + public boolean matches(Method method) { |
| 42 | + return method.getDeclaringClass() == Object.class; |
| 43 | + } |
| 44 | + }; |
| 45 | + protected static final AbstractMatcher<Method> SYNTHETIC = new AbstractMatcher<Method>() { |
| 46 | + @Override |
| 47 | + public boolean matches(Method method) { |
| 48 | + return method.isSynthetic(); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + private ClassLoader resourcesClassLoader = getDefaultClassLoader(); |
| 53 | + |
| 54 | + private ClassLoader driverClassLoader = getDefaultClassLoader(); |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritDoc} |
| 58 | + */ |
| 59 | + @Override |
| 60 | + protected final void configure() { |
| 61 | + try { |
| 62 | + // sql session manager |
| 63 | + bind(SqlSessionManager.class).toProvider(SqlSessionManagerProvider.class).in(Scopes.SINGLETON); |
| 64 | + bind(SqlSession.class).to(SqlSessionManager.class).in(Scopes.SINGLETON); |
| 65 | + |
| 66 | + internalConfigure(); |
| 67 | + |
| 68 | + bindTransactionInterceptors(); |
| 69 | + |
| 70 | + bind(ClassLoader.class) |
| 71 | + .annotatedWith(named("JDBC.driverClassLoader")) |
| 72 | + .toInstance(driverClassLoader); |
| 73 | + } finally { |
| 74 | + resourcesClassLoader = getDefaultClassLoader(); |
| 75 | + driverClassLoader = getDefaultClassLoader(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * bind transactional interceptors |
| 81 | + */ |
| 82 | + protected void bindTransactionInterceptors() { |
| 83 | + // transactional interceptor |
| 84 | + TransactionalMethodInterceptor interceptor = new TransactionalMethodInterceptor(); |
| 85 | + requestInjection(interceptor); |
| 86 | + bindInterceptor(any(), not(SYNTHETIC).and(not(DECLARED_BY_OBJECT)).and(annotatedWith(Transactional.class)), interceptor); |
| 87 | + // Intercept classes annotated with Transactional, but avoid "double" |
| 88 | + // interception when a mathod is also annotated inside an annotated |
| 89 | + // class. |
| 90 | + bindInterceptor(annotatedWith(Transactional.class), not(SYNTHETIC).and(not(DECLARED_BY_OBJECT)).and(not(annotatedWith(Transactional.class))), interceptor); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * |
| 95 | + * @param mapperType |
| 96 | + */ |
| 97 | + final <T> void bindMapper(Class<T> mapperType) { |
| 98 | + bind(mapperType).toProvider(guicify(new MapperProvider<T>(mapperType))).in(Scopes.SINGLETON); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * |
| 103 | + * @return |
| 104 | + * @since 3.3 |
| 105 | + */ |
| 106 | + public void useResourceClassLoader(ClassLoader resourceClassLoader) { |
| 107 | + this.resourcesClassLoader = resourceClassLoader; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * |
| 112 | + * @return |
| 113 | + * @since 3.3 |
| 114 | + */ |
| 115 | + protected final ClassLoader getResourceClassLoader() { |
| 116 | + return resourcesClassLoader; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * |
| 121 | + * @return |
| 122 | + * @since 3.3 |
| 123 | + */ |
| 124 | + public void useJdbcDriverClassLoader(ClassLoader driverClassLoader) { |
| 125 | + this.driverClassLoader = driverClassLoader; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * |
| 130 | + * @return |
| 131 | + * @since 3.3 |
| 132 | + */ |
| 133 | + private ClassLoader getDefaultClassLoader() { |
| 134 | + return getClass().getClassLoader(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Configures a {@link Binder} via the exposed methods. |
| 139 | + */ |
| 140 | + abstract void internalConfigure(); |
| 141 | + |
| 142 | + /** |
| 143 | + * |
| 144 | + */ |
| 145 | + protected abstract void initialize(); |
| 146 | + |
| 147 | +} |
0 commit comments