Skip to content

Commit 929a222

Browse files
committed
完善和Spring的集成,完善Bean的初始化和加载,fixed #51
1 parent 146aa0b commit 929a222

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

activerecord/src/main/java/io/mybatis/activerecord/spring/MapperProvider.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import io.mybatis.mapper.BaseMapper;
2020
import io.mybatis.provider.EntityClassFinder;
2121
import org.mybatis.spring.SqlSessionTemplate;
22+
import org.springframework.beans.BeansException;
2223
import org.springframework.context.ApplicationContext;
24+
import org.springframework.context.ApplicationContextAware;
2325
import org.springframework.context.ApplicationListener;
2426
import org.springframework.context.event.ContextRefreshedEvent;
2527

@@ -35,7 +37,7 @@
3537
* @param <M> 实体对应的 Mapper
3638
* @author liuzh
3739
*/
38-
public class MapperProvider<T, I extends Serializable, M extends BaseMapper<T, I>> implements ApplicationListener<ContextRefreshedEvent> {
40+
public class MapperProvider<T, I extends Serializable, M extends BaseMapper<T, I>> implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> {
3941
/**
4042
* Spring 上下文
4143
*/
@@ -76,9 +78,17 @@ public static <T, I extends Serializable, M extends BaseMapper<T, I>> MapperProv
7678
return (MapperProvider<T, I, M>) applicationContext.getBean(instanceName);
7779
}
7880

81+
@Override
82+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
83+
MapperProvider.applicationContext = applicationContext;
84+
}
85+
7986
@Override
8087
public void onApplicationEvent(ContextRefreshedEvent event) {
81-
MapperProvider.applicationContext = event.getApplicationContext();
88+
this.initMapper();
89+
}
90+
91+
protected void initMapper() {
8292
this.sqlSessionTemplate.getConfiguration().getMapperRegistry().getMappers().forEach(mapper -> {
8393
addMapper(mapper, this.sqlSessionTemplate.getMapper(mapper));
8494
});
@@ -91,9 +101,9 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
91101
* @param mapper Mapper 实例
92102
*/
93103
public void addMapper(Class<?> type, Object mapper) {
94-
if (type != null && BaseMapper.class.isAssignableFrom(type)) {
104+
if (type != null && mapper != null && BaseMapper.class.isAssignableFrom(type)) {
95105
EntityClassFinder.find(type, null).ifPresent(clazz -> {
96-
if (mapper != null) {
106+
if (!modelMapper.containsKey(clazz)) {
97107
modelMapper.put(clazz, (BaseMapper<T, I>) mapper);
98108
}
99109
});
@@ -107,6 +117,13 @@ public void addMapper(Class<?> type, Object mapper) {
107117
* @return Mapper 接口
108118
*/
109119
public M baseMapper(Class<T> modelClass) {
120+
if (!modelMapper.containsKey(modelClass)) {
121+
synchronized (this) {
122+
if (!modelMapper.containsKey(modelClass)) {
123+
this.initMapper();
124+
}
125+
}
126+
}
110127
if (modelMapper.containsKey(modelClass)) {
111128
return (M) modelMapper.get(modelClass);
112129
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.mybatis.activerecord.spring;
2+
3+
import java.util.Optional;
4+
5+
public class SystemTask {
6+
7+
public void run() {
8+
Optional<User> user = new User().baseMapper().wrapper().eq(User::getId, 1).one();
9+
user.ifPresent(u -> System.out.println(u.getName()));
10+
}
11+
12+
}

activerecord/src/test/java/io/mybatis/activerecord/spring/spring.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1919
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
2020
xmlns="http://www.springframework.org/schema/beans"
21+
xmlns:task="http://www.springframework.org/schema/task"
2122
xsi:schemaLocation="http://www.springframework.org/schema/beans
2223
http://www.springframework.org/schema/beans/spring-beans.xsd
23-
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
24+
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
25+
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
2426
default-lazy-init="false">
2527

2628
<jdbc:embedded-database id="dataSourceRole">
@@ -31,6 +33,10 @@
3133
<jdbc:script location="classpath:io/mybatis/activerecord/spring/database-schema-user.sql"/>
3234
</jdbc:embedded-database>
3335

36+
<task:scheduler id="myScheduler"/>
37+
38+
<bean id="systemTask" class="io.mybatis.activerecord.spring.SystemTask"/>
39+
3440
<bean id="sqlSessionFactoryRole" class="org.mybatis.spring.SqlSessionFactoryBean">
3541
<property name="dataSource" ref="dataSourceRole"/>
3642
</bean>
@@ -76,4 +82,8 @@
7682
<property name="markerInterface" value="io.mybatis.activerecord.spring.UserMarker"/>
7783
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryUser"/>
7884
</bean>
85+
86+
<task:scheduled-tasks scheduler="myScheduler">
87+
<task:scheduled ref="systemTask" method="run" fixed-delay="5000"/>
88+
</task:scheduled-tasks>
7989
</beans>

0 commit comments

Comments
 (0)