Skip to content

Commit 98faf0e

Browse files
committed
Add EntityListenerProvider auto configuration
1 parent ed656fe commit 98faf0e

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.seasar.doma.boot.autoconfigure;
1717

1818
import org.seasar.doma.jdbc.Config;
19+
import org.seasar.doma.jdbc.EntityListenerProvider;
1920
import org.seasar.doma.jdbc.Naming;
2021
import org.seasar.doma.jdbc.SqlFileRepository;
2122
import org.seasar.doma.jdbc.dialect.Dialect;
@@ -27,6 +28,7 @@
2728
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
2829
import org.springframework.boot.context.properties.ConfigurationProperties;
2930
import org.springframework.boot.context.properties.EnableConfigurationProperties;
31+
import org.springframework.context.ApplicationContext;
3032
import org.springframework.context.annotation.Bean;
3133
import org.springframework.context.annotation.Configuration;
3234
import org.springframework.dao.support.PersistenceExceptionTranslator;
@@ -77,6 +79,13 @@ public Naming naming() {
7779
}
7880

7981
@Bean
82+
@ConditionalOnMissingBean
83+
public EntityListenerProvider entityListenerProvider(
84+
ApplicationContext context) {
85+
return new TryLookupEntityListenerProvider(context);
86+
}
87+
88+
@Bean
8089
@ConditionalOnMissingBean
8190
public DomaConfigBuilder domaConfigBuilder() {
8291
return new DomaConfigBuilder();
@@ -87,6 +96,7 @@ public DomaConfigBuilder domaConfigBuilder() {
8796
@ConfigurationProperties(prefix = DOMA_PREFIX)
8897
public DomaConfig config(DataSource dataSource, Dialect dialect,
8998
SqlFileRepository sqlFileRepository, Naming naming,
99+
EntityListenerProvider entityListenerProvider,
90100
DomaConfigBuilder domaConfigBuilder) {
91101
if (domaConfigBuilder.dataSource() == null) {
92102
domaConfigBuilder.dataSource(dataSource);
@@ -100,6 +110,9 @@ public DomaConfig config(DataSource dataSource, Dialect dialect,
100110
if (domaConfigBuilder.naming() == null) {
101111
domaConfigBuilder.naming(naming);
102112
}
113+
if (domaConfigBuilder.entityListenerProvider() == null) {
114+
domaConfigBuilder.entityListenerProvider(entityListenerProvider);
115+
}
103116
return new DomaConfig(domaConfigBuilder);
104117
}
105118

doma-spring-boot-autoconfigure/src/main/java/org/seasar/doma/boot/autoconfigure/DomaConfigBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class DomaConfigBuilder {
4848
private Naming naming;
4949
private MapKeyNaming mapKeyNaming = ConfigSupport.defaultMapKeyNaming;
5050
private Commenter commenter = ConfigSupport.defaultCommenter;
51-
private EntityListenerProvider entityListenerProvider = ConfigSupport.defaultEntityListenerProvider;
51+
private EntityListenerProvider entityListenerProvider;
5252

5353
public DataSource dataSource() {
5454
return dataSource;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2004-2016 the Seasar Foundation and the Others.
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,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.boot.autoconfigure;
17+
18+
import java.util.Map;
19+
import java.util.Objects;
20+
import java.util.function.Supplier;
21+
22+
import org.seasar.doma.jdbc.EntityListenerProvider;
23+
import org.seasar.doma.jdbc.entity.EntityListener;
24+
import org.springframework.context.ApplicationContext;
25+
26+
/**
27+
* {@link EntityListenerProvider} implementation that
28+
* {@link EntityListener} managed by Spring Framework, or else created by Doma.
29+
*
30+
* @author backpaper0
31+
*
32+
*/
33+
public class TryLookupEntityListenerProvider implements EntityListenerProvider {
34+
35+
private final ApplicationContext context;
36+
37+
public TryLookupEntityListenerProvider(ApplicationContext context) {
38+
this.context = Objects.requireNonNull(context);
39+
}
40+
41+
@Override
42+
public <ENTITY, LISTENER extends EntityListener<ENTITY>> LISTENER get(
43+
Class<LISTENER> listenerClass, Supplier<LISTENER> listenerSupplier) {
44+
Map<String, LISTENER> beans = context.getBeansOfType(listenerClass);
45+
return beans.values().stream().findAny().orElseGet(listenerSupplier);
46+
}
47+
}

doma-spring-boot-autoconfigure/src/test/java/org/seasar/doma/boot/autoconfigure/DomaAutoConfigurationTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public void testAutoRegisteredConfig() {
6767
is(instanceOf(GreedyCacheSqlFileRepository.class)));
6868
assertThat(config.getNaming(), is(Naming.DEFAULT));
6969
assertThat(config.getJdbcLogger(), is(instanceOf(UtilLoggingJdbcLogger.class)));
70+
assertThat(config.getEntityListenerProvider(),
71+
is(instanceOf(TryLookupEntityListenerProvider.class)));
7072
PersistenceExceptionTranslator translator = this.context
7173
.getBean(PersistenceExceptionTranslator.class);
7274
assertThat(translator, is(instanceOf(DomaPersistenceExceptionTranslator.class)));
@@ -86,6 +88,8 @@ public void testConfigWithDomaConfigBuilder() {
8688
is(instanceOf(NoCacheSqlFileRepository.class)));
8789
assertThat(config.getNaming(), is(Naming.SNAKE_UPPER_CASE));
8890
assertThat(config.getJdbcLogger(), is(instanceOf(UtilLoggingJdbcLogger.class)));
91+
assertThat(config.getEntityListenerProvider(),
92+
is(instanceOf(TestEntityListenerProvider.class)));
8993
PersistenceExceptionTranslator translator = this.context
9094
.getBean(PersistenceExceptionTranslator.class);
9195
assertThat(translator, is(instanceOf(DomaPersistenceExceptionTranslator.class)));
@@ -105,6 +109,8 @@ public void testConfigWithConfig() {
105109
is(instanceOf(NoCacheSqlFileRepository.class)));
106110
assertThat(config.getNaming(), is(Naming.SNAKE_LOWER_CASE));
107111
assertThat(config.getJdbcLogger(), is(instanceOf(UtilLoggingJdbcLogger.class)));
112+
assertThat(config.getEntityListenerProvider(),
113+
is(instanceOf(TestEntityListenerProvider.class)));
108114
PersistenceExceptionTranslator translator = this.context
109115
.getBean(PersistenceExceptionTranslator.class);
110116
assertThat(translator, is(instanceOf(DomaPersistenceExceptionTranslator.class)));
@@ -178,7 +184,8 @@ public static class ConfigBuilderConfigure {
178184
DomaConfigBuilder myDomaConfigBuilder() {
179185
return new DomaConfigBuilder().dialect(new MysqlDialect())
180186
.sqlFileRepository(new NoCacheSqlFileRepository())
181-
.naming(Naming.SNAKE_UPPER_CASE);
187+
.naming(Naming.SNAKE_UPPER_CASE)
188+
.entityListenerProvider(new TestEntityListenerProvider());
182189
}
183190
}
184191

@@ -206,8 +213,15 @@ public SqlFileRepository getSqlFileRepository() {
206213
public Naming getNaming() {
207214
return Naming.SNAKE_LOWER_CASE;
208215
}
216+
217+
@Override
218+
public EntityListenerProvider getEntityListenerProvider() {
219+
return new TestEntityListenerProvider();
220+
}
209221
};
210222
}
211223
}
212224

225+
static class TestEntityListenerProvider implements EntityListenerProvider {
226+
}
213227
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2004-2016 the Seasar Foundation and the Others.
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,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.boot.autoconfigure;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.hamcrest.MatcherAssert.*;
20+
21+
import org.junit.Test;
22+
import org.seasar.doma.jdbc.entity.EntityListener;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.stereotype.Component;
27+
28+
public class TryLookupEntityListenerProviderTest {
29+
30+
@Test
31+
public void testManaged() throws Exception {
32+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
33+
context.register(FooListener.class);
34+
context.refresh();
35+
TryLookupEntityListenerProvider provider = new TryLookupEntityListenerProvider(
36+
context);
37+
FooListener listener = provider
38+
.get(FooListener.class, FooListener::new);
39+
assertThat(listener.managed, is(true));
40+
}
41+
42+
@Test
43+
public void testNotManaged() throws Exception {
44+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
45+
//context.register(FooListener.class);
46+
context.refresh();
47+
TryLookupEntityListenerProvider provider = new TryLookupEntityListenerProvider(
48+
context);
49+
FooListener listener = provider
50+
.get(FooListener.class, FooListener::new);
51+
assertThat(listener.managed, is(false));
52+
}
53+
54+
@Component
55+
public static class FooListener implements EntityListener<Object> {
56+
57+
final boolean managed;
58+
59+
//Invoked by Doma
60+
public FooListener() {
61+
managed = false;
62+
}
63+
64+
//Invoked by Spring
65+
@Autowired
66+
public FooListener(ApplicationContext context) {
67+
managed = true;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)