<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mybatis/mapper/ms/*.xml" />
<property name="typeAliasesPackage" value="cn.freemethod.to" />
<property name="configLocation" value="classpath:mybatis/config.xml" />
</bean>
在使用Spring和MyBatis集成的时候,配置了如上所示的bean,这个bean对应的类是SqlSessionFactoryBean,这个类实现了FactoryBean,这里不详细介绍这个类,我们只是看一看它的mapperLocations和configLocation两个属性。
图1 SQLSessionFactoryBean
如上图所示,我们可以看到 mapperLocations和configLocation两个属性是Resource类型,spring是怎样处理的呢?怎样把一个字符串转换为一个Resource类型的呢?这里就要介绍一下PropertyEditor这个接口了。我们还是先来看一个例子在来看上面的问题。
先来一个类,我们还是使用User这类吧:
public class User {
private Integer id;
private String name;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}
UserPropertyEditor继承PropertyEditorSupport,主要重写了setAsText方法和getAsText方法。PropertyEditorSupport实现了PropertyEditor接口。
import java.beans.PropertyEditorSupport;
import cn.freemethod.to.User;
public class UserPropertyEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
String[] fields = text.split(",");
if(fields.length != 3)
throw new IllegalArgumentException("User 属性配置错误");
User user = new User();
try {
int id = Integer.parseInt(fields[0]);
user.setId(id);
} catch (Exception e) {
throw new IllegalArgumentException("User的属性配置错误");
}
user.setName(fields[1]);
user.setAddress(fields[2]);
setValue(user);
}
@Override
public String getAsText() {
User user = (User) getValue();
return user.toString();
}
}
配置文件,主要是把我们自定义的UserPropertyEditor注册到CustomerEditorConfigurer中这样当Spring在处理配置文件的时候发现需要把对应的String类型转换成对应类型就可以是我们注册在 CustomerEditorConfigurer 的PropertyEditor了。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="cn.freemethod.to.User" value="cn.freemethod.test.UserPropertyEditor" />
</map>
</property>
</bean>
<bean id="start" class="cn.freemethod.test.Start">
<property name="user" value="1,tim,chengdu" />
</bean>
</beans>
主类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.freemethod.to.User;
public class Start {
private User user;
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("test.xml");
Start start = (Start) context.getBean("start");
System.out.println(start.user);
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
测试结果:
图2 测试结果
从测试结果可以看到Spring利用UserPropertyEditor成功把特定的字符串类型转换为了User类型。
同样的道理,也可以把字符串转换为Resource类型,Spring已经为我们提供了一个ResourceEditor类,所以我们在SqlSessionFactoryBean中可以把字符串配置转换为Resource类型。下面是ResourceEditor的源码。
package org.springframework.core.io;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
public class ResourceEditor extends PropertyEditorSupport {
private final ResourceLoader resourceLoader;
private PropertyResolver propertyResolver;
private final boolean ignoreUnresolvablePlaceholders;
public ResourceEditor() {
this(new DefaultResourceLoader(), null);
}
@Deprecated
public ResourceEditor(ResourceLoader resourceLoader) {
this(resourceLoader, null, true);
}
@Deprecated
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
this(resourceLoader, null, ignoreUnresolvablePlaceholders);
}
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
this(resourceLoader, propertyResolver, true);
}
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.resourceLoader = resourceLoader;
this.propertyResolver = propertyResolver;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
}
@Override
public void setAsText(String text) {
if (StringUtils.hasText(text)) {
String locationToUse = resolvePath(text).trim();
setValue(this.resourceLoader.getResource(locationToUse));
}
else {
setValue(null);
}
}
protected String resolvePath(String path) {
if (this.propertyResolver == null) {
this.propertyResolver = new StandardEnvironment();
}
return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path));
}
@Override
public String getAsText() {
Resource value = (Resource) getValue();
try {
return (value != null ? value.getURL().toExternalForm() : "");
}
catch (IOException ex) {
return null;
}
}
}