-
-
Notifications
You must be signed in to change notification settings - Fork 972
Keep BeanBuilder DSL independent of XML setup #15824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamesfredley
wants to merge
7
commits into
8.0.x
Choose a base branch
from
fix/14915-beanbuilder-spring7
base: 8.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9943118
Keep BeanBuilder DSL independent of XML setup
833d3e4
Avoid XML reader init for missing namespaces
jamesfredley 5b3d004
Merge remote-tracking branch 'origin/8.0.x' into fix/14915-beanbuilde…
jamesfredley 0f1cf52
Merge 8.0.x into fix/14915-beanbuilder-spring7
jamesfredley a3b9109
Merge remote-tracking branch 'origin/8.0.x' into fix/14915-beanbuilde…
jamesfredley a1b7309
Merge origin/8.0.x into fix/14915-beanbuilder-spring7
jamesfredley c47fb1e
Merge branch '8.0.x' into fix/14915-beanbuilder-spring7
jamesfredley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
grails-spring/src/main/groovy/grails/spring/BeanBuilderXmlSupport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.spring; | ||
|
|
||
| import org.springframework.beans.factory.parsing.EmptyReaderEventListener; | ||
| import org.springframework.beans.factory.parsing.FailFastProblemReporter; | ||
| import org.springframework.beans.factory.parsing.NullSourceExtractor; | ||
| import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
| import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry; | ||
| import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate; | ||
| import org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver; | ||
| import org.springframework.beans.factory.xml.NamespaceHandler; | ||
| import org.springframework.beans.factory.xml.NamespaceHandlerResolver; | ||
| import org.springframework.beans.factory.xml.ParserContext; | ||
| import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; | ||
| import org.springframework.beans.factory.xml.XmlReaderContext; | ||
| import org.springframework.context.support.GenericApplicationContext; | ||
| import org.springframework.core.io.Resource; | ||
|
|
||
| import org.grails.spring.RuntimeSpringConfiguration; | ||
|
|
||
| final class BeanBuilderXmlSupport { | ||
|
|
||
| private final RuntimeSpringConfiguration springConfig; | ||
| private ClassLoader classLoader; | ||
| private NamespaceHandlerResolver namespaceHandlerResolver; | ||
| private XmlBeanDefinitionReader xmlBeanDefinitionReader; | ||
| private XmlReaderContext readerContext; | ||
| private Resource readerContextResource; | ||
|
|
||
| BeanBuilderXmlSupport(RuntimeSpringConfiguration springConfig, ClassLoader classLoader) { | ||
| this.springConfig = springConfig; | ||
| setClassLoader(classLoader); | ||
| } | ||
|
|
||
| void setClassLoader(ClassLoader classLoader) { | ||
| this.classLoader = classLoader; | ||
| namespaceHandlerResolver = new DefaultNamespaceHandlerResolver(classLoader); | ||
| xmlBeanDefinitionReader = null; | ||
| readerContext = null; | ||
| readerContextResource = null; | ||
| } | ||
|
|
||
| void setNamespaceHandlerResolver(NamespaceHandlerResolver namespaceHandlerResolver) { | ||
| this.namespaceHandlerResolver = namespaceHandlerResolver; | ||
| readerContext = null; | ||
| readerContextResource = null; | ||
| } | ||
|
|
||
| NamespaceHandler resolveNamespaceHandler(String uri) { | ||
| return namespaceHandlerResolver.resolve(uri); | ||
| } | ||
|
|
||
| ParserContext createParserContext(Resource resource) { | ||
| XmlReaderContext currentReaderContext = getReaderContext(resource); | ||
| return new ParserContext(currentReaderContext, new BeanDefinitionParserDelegate(currentReaderContext)); | ||
| } | ||
|
|
||
| XmlReaderContext getReaderContext(Resource resource) { | ||
| if (readerContext == null || !resource.equals(readerContextResource)) { | ||
| readerContext = new XmlReaderContext(resource, new FailFastProblemReporter(), new EmptyReaderEventListener(), | ||
| new NullSourceExtractor(), getXmlBeanDefinitionReader(), namespaceHandlerResolver); | ||
| readerContextResource = resource; | ||
| } | ||
| return readerContext; | ||
| } | ||
|
|
||
| BeanDefinitionRegistry loadBeanDefinitions(Resource resource) { | ||
| SimpleBeanDefinitionRegistry beanRegistry = new SimpleBeanDefinitionRegistry(); | ||
| XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(beanRegistry); | ||
| beanReader.setBeanClassLoader(classLoader); | ||
| beanReader.loadBeanDefinitions(resource); | ||
| return beanRegistry; | ||
| } | ||
|
|
||
| private XmlBeanDefinitionReader getXmlBeanDefinitionReader() { | ||
| if (xmlBeanDefinitionReader == null) { | ||
| xmlBeanDefinitionReader = new XmlBeanDefinitionReader((GenericApplicationContext) springConfig.getUnrefreshedApplicationContext()); | ||
| xmlBeanDefinitionReader.setBeanClassLoader(classLoader); | ||
| } | ||
| return xmlBeanDefinitionReader; | ||
| } | ||
| } |
105 changes: 105 additions & 0 deletions
105
grails-spring/src/test/groovy/grails/spring/BeanBuilderTests.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package grails.spring | ||
|
|
||
| import org.grails.spring.DefaultRuntimeSpringConfiguration | ||
| import org.junit.jupiter.api.Test | ||
| import org.springframework.context.ApplicationContext | ||
| import org.springframework.core.io.ByteArrayResource | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals | ||
| import static org.junit.jupiter.api.Assertions.assertTrue | ||
|
|
||
| class BeanBuilderTests { | ||
|
|
||
| @Test | ||
| void testPlainBeanBuilderDslDoesNotInitializeXmlSupport() { | ||
| def springConfig = new DefaultRuntimeSpringConfiguration() { | ||
| @Override | ||
| ApplicationContext getUnrefreshedApplicationContext() { | ||
| throw new AssertionError('XML support should not be initialized for plain BeanBuilder DSL') | ||
| } | ||
| } | ||
| def beanBuilder = new BeanBuilder(null, springConfig, getClass().classLoader) | ||
|
|
||
| beanBuilder.beans { | ||
| bean1(Bean1) { | ||
| person = 'homer' | ||
| } | ||
| } | ||
|
|
||
| assertTrue springConfig.containsBean('bean1') | ||
| assertEquals Bean1, springConfig.createBeanDefinition('bean1').beanClass | ||
| } | ||
|
|
||
| @Test | ||
| void testImportBeansXmlInitializesXmlSupportOnDemand() { | ||
| def springConfig = new DefaultRuntimeSpringConfiguration() | ||
| def beanBuilder = new BeanBuilder(null, springConfig, getClass().classLoader) | ||
|
|
||
| beanBuilder.beans { | ||
| importBeans new NamedByteArrayResource(''' | ||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> | ||
| <bean id="xmlBean" class="java.lang.String"> | ||
| <constructor-arg value="hello"/> | ||
| </bean> | ||
| </beans> | ||
| '''.bytes, 'test.xml') | ||
| } | ||
|
|
||
| assertTrue springConfig.containsBean('xmlBean') | ||
| assertEquals String, springConfig.createBeanDefinition('xmlBean').beanClass | ||
| } | ||
|
|
||
| @Test | ||
| void testXmlnsInitializesNamespaceSupportOnDemand() { | ||
| def beanBuilder = new BeanBuilder() | ||
|
|
||
| beanBuilder.beans { | ||
| xmlns util: 'http://www.springframework.org/schema/util' | ||
|
|
||
| util.list(id: 'letters') { | ||
| value 'one' | ||
| value 'two' | ||
| } | ||
| } | ||
|
|
||
| assertEquals ['one', 'two'], beanBuilder.createApplicationContext().getBean('letters') | ||
| } | ||
|
|
||
| static class Bean1 { | ||
| String person | ||
| } | ||
|
|
||
| private static class NamedByteArrayResource extends ByteArrayResource { | ||
| private final String filename | ||
|
|
||
| NamedByteArrayResource(byte[] byteArray, String filename) { | ||
| super(byteArray) | ||
| this.filename = filename | ||
| } | ||
|
|
||
| @Override | ||
| String getFilename() { | ||
| filename | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.