Skip to content

Commit b95783e

Browse files
committed
fix for http://code.google.com/p/mybatis/issues/detail?id=317 . Loading more than one file with the same namespace should work.
1 parent 1f548b6 commit b95783e

File tree

5 files changed

+164
-113
lines changed

5 files changed

+164
-113
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,37 @@ public MapperAnnotationBuilder(Configuration configuration, Class<?> type) {
8787
}
8888

8989
public void parse() {
90-
String namespace = type.getName();
91-
assistant.setCurrentNamespace(namespace);
92-
if (!configuration.isResourceLoaded(namespace)) {
90+
String resource = type.toString();
91+
if (!configuration.isResourceLoaded(resource)) {
9392
loadXmlResource();
94-
}
95-
parseCache();
96-
parseCacheRef();
97-
Method[] methods = type.getMethods();
98-
for (Method method : methods) {
99-
parseResultsAndConstructorArgs(method);
100-
parseStatement(method);
93+
configuration.addLoadedResource(resource);
94+
assistant.setCurrentNamespace(type.getName());
95+
parseCache();
96+
parseCacheRef();
97+
Method[] methods = type.getMethods();
98+
for (Method method : methods) {
99+
parseResultsAndConstructorArgs(method);
100+
parseStatement(method);
101+
}
101102
}
102103
}
103104

104105
private void loadXmlResource() {
105-
String xmlResource = type.getName().replace('.', '/') + ".xml";
106-
InputStream inputStream = null;
107-
try {
108-
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
109-
} catch (IOException e) {
110-
// ignore, resource is not required
111-
}
112-
if (inputStream != null) {
113-
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
114-
xmlParser.parse();
106+
// Spring may not know the real resource name so we check a flag
107+
// to prevent loading again a resource twice
108+
// this flag is set at XMLMapperBuilder#bindMapperForNamespace
109+
if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
110+
String xmlResource = type.getName().replace('.', '/') + ".xml";
111+
InputStream inputStream = null;
112+
try {
113+
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
114+
} catch (IOException e) {
115+
// ignore, resource is not required
116+
}
117+
if (inputStream != null) {
118+
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
119+
xmlParser.parse();
120+
}
115121
}
116122
}
117123

src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Properties;
1313

1414
import org.apache.ibatis.builder.BaseBuilder;
15+
import org.apache.ibatis.builder.BuilderException;
1516
import org.apache.ibatis.builder.CacheRefResolver;
1617
import org.apache.ibatis.builder.IncompleteCacheException;
1718
import org.apache.ibatis.builder.MapperBuilderAssistant;
@@ -35,6 +36,7 @@ public class XMLMapperBuilder extends BaseBuilder {
3536
private XPathParser parser;
3637
private MapperBuilderAssistant builderAssistant;
3738
private Map<String, XNode> sqlFragments;
39+
private String resource;
3840

3941
@Deprecated
4042
public XMLMapperBuilder(Reader reader, Configuration configuration, String resource, Map<String, XNode> sqlFragments, String namespace) {
@@ -63,27 +65,28 @@ private XMLMapperBuilder(XPathParser parser, Configuration configuration, String
6365
this.builderAssistant = new MapperBuilderAssistant(configuration, resource);
6466
this.parser = parser;
6567
this.sqlFragments = sqlFragments;
68+
this.resource = resource;
6669
}
67-
70+
6871
public void parse() {
69-
XNode context = parser.evalNode("/mapper");
70-
String namespace = context.getStringAttribute("namespace");
71-
builderAssistant.setCurrentNamespace(namespace);
72-
if (!configuration.isResourceLoaded(namespace)) {
73-
configurationElement(context);
74-
configuration.addLoadedResource(namespace);
72+
if (!configuration.isResourceLoaded(resource)) {
73+
configurationElement(parser.evalNode("/mapper"));
74+
configuration.addLoadedResource(resource);
7575
bindMapperForNamespace();
76-
parsePendingChacheRefs();
77-
parsePendingStatements();
7876
}
77+
78+
parsePendingChacheRefs();
79+
parsePendingStatements();
7980
}
80-
81+
8182
public XNode getSqlFragment(String refid) {
8283
return sqlFragments.get(refid);
8384
}
8485

8586
private void configurationElement(XNode context) {
8687
try {
88+
String namespace = context.getStringAttribute("namespace");
89+
builderAssistant.setCurrentNamespace(namespace);
8790
cacheRefElement(context.evalNode("cache-ref"));
8891
cacheElement(context.evalNode("cache"));
8992
parameterMapElement(context.evalNodes("/mapper/parameterMap"));
@@ -93,7 +96,6 @@ private void configurationElement(XNode context) {
9396
} catch (Exception e) {
9497
throw new RuntimeException("Error parsing Mapper XML. Cause: " + e, e);
9598
}
96-
9799
}
98100

99101
private void buildStatementFromContext(List<XNode> list) {
@@ -305,6 +307,10 @@ private void bindMapperForNamespace() {
305307
}
306308
if (boundType != null) {
307309
if (!configuration.hasMapper(boundType)) {
310+
// Spring may not know the real resource name so we set a flag
311+
// to prevent loading again this resource from the mapper interface
312+
// look at MapperAnnotationBuilder#loadXmlResource
313+
configuration.addLoadedResource("namespace:" + namespace);
308314
configuration.addMapper(boundType);
309315
}
310316
}

0 commit comments

Comments
 (0)