在1.2.5以下版本的mybatis-spring一下配置可能会无法启动,但junit方式可以启动
配置:
<bean id="sqlSessionFactoryName" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="${dao.mybatis.sqlSessionFactory.configLocation}" />
</bean>
经分析1.2.5以下的mybatis-spring的MapperScannerConfigurer中判断是否为GenericApplicationContext的实例
启动时不为GenericApplicationContext的实例时属性无法加载
1.3.1以后版本已经修改
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--以下两个参数解决连接池属性无法加载问题-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryName"/>
<property name="processPropertyPlaceHolders" value="true"/>
<property name="basePackage" value="${dao.mybatis.scanner.basePackage}" />
</bean>
到此Mybatis本身的属性已经可以正常加载了,但在正式环境中可能会碰到xml中其他注入属性无法加载,如Dubbo的配置信息注入
经分析原因在于MapperScannerConfigurer中的代码有问题。
private void processPropertyPlaceHolders() {
Map prcs = this.applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
if(!prcs.isEmpty() && this.applicationContext instanceof ConfigurableApplicationContext) {
红色这一行代码会导致在属性加载前加载dubbo的xml文件中定义的bean,导致属性未注入
解决方法如下:
新建对象继承MapperScannerConfigurer,注入自定义属性在开始扫描前加载,示例如下:
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.PropertyResourceConfigurer;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
*
*/
public class SelfMapperScannerConfigurer extends MapperScannerConfigurer {
private ApplicationContext applicationContext;
private String beanName;
private String preLoadPropBeanName;
public void setApplicationContext(ApplicationContext applicationContext) {
super.setApplicationContext(applicationContext);
this.applicationContext = applicationContext;
}
@Override
public void setBeanName(String name) {
this.beanName = name;
super.setBeanName(name);
}
public void setPreLoadPropBeanName(String preLoadPropBeanName) {
this.preLoadPropBeanName = preLoadPropBeanName;
}
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if (this.preLoadPropBeanName != null) {
preloadPropertyResource();
this.setProcessPropertyPlaceHolders(false);
}
super.postProcessBeanDefinitionRegistry(registry);
}
private void preloadPropertyResource() {
if (applicationContext.containsBean(this.preLoadPropBeanName)) {
BeanDefinition mapperScannerBean = ((ConfigurableApplicationContext) this.applicationContext).getBeanFactory().getBeanDefinition(this.beanName);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerBeanDefinition(this.beanName, mapperScannerBean);
PropertyResourceConfigurer configurer = applicationContext.getBean(this.preLoadPropBeanName, PropertyResourceConfigurer.class);
configurer.postProcessBeanFactory(factory);
if (applicationContext.containsBean("propertyConfigurer")) {
configurer = applicationContext.getBean(this.preLoadPropBeanName, PropertyResourceConfigurer.class);
configurer.postProcessBeanFactory(factory);
}
MutablePropertyValues values1 = mapperScannerBean.getPropertyValues();
String basePackage = this.updatePropertyValue("basePackage", values1);
String sqlSessionFactoryBeanName = this.updatePropertyValue("sqlSessionFactoryBeanName", values1);
String sqlSessionTemplateBeanName = this.updatePropertyValue("sqlSessionTemplateBeanName", values1);
this.setBasePackage(basePackage);
this.setSqlSessionFactoryBeanName(sqlSessionFactoryBeanName);
this.setSqlSessionTemplateBeanName(sqlSessionTemplateBeanName);
}
}
private String updatePropertyValue(String propertyName, PropertyValues values) {
PropertyValue property = values.getPropertyValue(propertyName);
String rst = null;
if (property == null) {
return null;
} else {
Object value = property.getValue();
rst = value == null ? null : (value instanceof String ? value.toString() : (value instanceof TypedStringValue ? ((TypedStringValue) value).getValue() : null));
}
return rst;
}
}
修改配置:
<bean id="mapperScannerConfigurer" class="XX.SelfMapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryName"/>
<property name="processPropertyPlaceHolders" value="true"/>
<property name="basePackage" value="${dao.mybatis.scanner.basePackage}" />
<!--mybatis需要的属性文件注入,会忽略掉processPropertyPlaceHolders配置-->
<property name="preLoadPropBeanName" value="propertyPlaceholderConfigurer"/>
</bean>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。