Skip to content

SPR-8335: fixed default-lazy-init processing when xsd validation is disabled #2071

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public class BeanDefinitionParserDelegate {

public static final String DEFAULT_VALUE = "default";

private static final String EMPTY_STRING = "";

public static final String DESCRIPTION_ELEMENT = "description";

public static final String AUTOWIRE_NO_VALUE = "no";
Expand Down Expand Up @@ -320,21 +322,21 @@ public void initDefaults(Element root, @Nullable BeanDefinitionParserDelegate pa
*/
protected void populateDefaults(DocumentDefaultsDefinition defaults, @Nullable DocumentDefaultsDefinition parentDefaults, Element root) {
String lazyInit = root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE);
if (DEFAULT_VALUE.equals(lazyInit)) {
if (isDefault(lazyInit)) {
// Potentially inherited from outer <beans> sections, otherwise falling back to false.
lazyInit = (parentDefaults != null ? parentDefaults.getLazyInit() : FALSE_VALUE);
}
defaults.setLazyInit(lazyInit);

String merge = root.getAttribute(DEFAULT_MERGE_ATTRIBUTE);
if (DEFAULT_VALUE.equals(merge)) {
if (isDefault(merge)) {
// Potentially inherited from outer <beans> sections, otherwise falling back to false.
merge = (parentDefaults != null ? parentDefaults.getMerge() : FALSE_VALUE);
}
defaults.setMerge(merge);

String autowire = root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE);
if (DEFAULT_VALUE.equals(autowire)) {
if (isDefault(autowire)) {
// Potentially inherited from outer <beans> sections, otherwise falling back to 'no'.
autowire = (parentDefaults != null ? parentDefaults.getAutowire() : AUTOWIRE_NO_VALUE);
}
Expand Down Expand Up @@ -572,7 +574,7 @@ else if (containingBean != null) {
}

String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
if (DEFAULT_VALUE.equals(lazyInit)) {
if (isDefault(lazyInit)) {
lazyInit = this.defaults.getLazyInit();
}
bd.setLazyInit(TRUE_VALUE.equals(lazyInit));
Expand All @@ -586,7 +588,7 @@ else if (containingBean != null) {
}

String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);
if ("".equals(autowireCandidate) || DEFAULT_VALUE.equals(autowireCandidate)) {
if (isDefault(autowireCandidate)) {
String candidatePattern = this.defaults.getAutowireCandidates();
if (candidatePattern != null) {
String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern);
Expand Down Expand Up @@ -661,7 +663,7 @@ public void parseMetaElements(Element ele, BeanMetadataAttributeAccessor attribu
@SuppressWarnings("deprecation")
public int getAutowireMode(String attValue) {
String att = attValue;
if (DEFAULT_VALUE.equals(att)) {
if (isDefault(att)) {
att = this.defaults.getAutowire();
}
int autowire = AbstractBeanDefinition.AUTOWIRE_NO;
Expand All @@ -681,6 +683,9 @@ else if (AUTOWIRE_AUTODETECT_VALUE.equals(att)) {
return autowire;
}

private boolean isDefault(String value) {
return (DEFAULT_VALUE.equals(value) || EMPTY_STRING.equals(value));
}
/**
* Parse constructor-arg sub-elements of the given bean element.
*/
Expand Down Expand Up @@ -1341,7 +1346,7 @@ public Properties parsePropsElement(Element propsEle) {
*/
public boolean parseMergeAttribute(Element collectionElement) {
String value = collectionElement.getAttribute(MERGE_ATTRIBUTE);
if (DEFAULT_VALUE.equals(value)) {
if (isDefault(value)) {
value = this.defaults.getMerge();
}
return TRUE_VALUE.equals(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public void defaultLazyInit() {
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass()));

assertLazyInits(bf);
}

private void assertLazyInits(DefaultListableBeanFactory bf) {
BeanDefinition foo = bf.getBeanDefinition("foo");
BeanDefinition bar = bf.getBeanDefinition("bar");
BeanDefinition baz = bf.getBeanDefinition("baz");
Expand All @@ -54,13 +58,40 @@ public void defaultLazyInit() {
assertThat(buz.isLazyInit(), is(true));
}

@Test
public void defaultLazyInitWithNonValidatingParser() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(bf);
xmlBeanDefinitionReader.setValidating(false);
xmlBeanDefinitionReader.loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass()));

assertLazyInits(bf);
}

@Test
@SuppressWarnings("unchecked")
public void defaultMerge() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-merge-context.xml", this.getClass()));

assertMerge(bf);
}

@Test
@SuppressWarnings("unchecked")
public void defaultMergeWithNonValidatingParser() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(bf);
xmlBeanDefinitionReader.setValidating(false);
xmlBeanDefinitionReader.loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-merge-context.xml", this.getClass()));

assertMerge(bf);
}

private void assertMerge(DefaultListableBeanFactory bf) {
TestBean topLevel = bf.getBean("topLevelConcreteTestBean", TestBean.class);
// has the concrete child bean values
assertThat((Iterable<String>) topLevel.getSomeList(), hasItems("charlie", "delta"));
Expand All @@ -74,7 +105,7 @@ public void defaultMerge() {

TestBean secondLevel = bf.getBean("secondLevelNestedTestBean", TestBean.class);
// merges all values
assertThat((Iterable<String>)secondLevel.getSomeList(),
assertThat((Iterable<String>) secondLevel.getSomeList(),
hasItems("charlie", "delta", "echo", "foxtrot", "golf", "hotel"));
}

Expand All @@ -84,6 +115,21 @@ public void defaultAutowireCandidates() {
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml", this.getClass()));

assertAutowireCandidates(bf);
}

@Test
public void defaultAutowireCandidatesWithNonValidatingParser() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(bf);
xmlBeanDefinitionReader.setValidating(false);
xmlBeanDefinitionReader.loadBeanDefinitions(
new ClassPathResource("NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml", this.getClass()));

assertAutowireCandidates(bf);
}

private void assertAutowireCandidates(DefaultListableBeanFactory bf) {
assertThat(bf.getBeanDefinition("fooService").isAutowireCandidate(), is(true));
assertThat(bf.getBeanDefinition("fooRepository").isAutowireCandidate(), is(true));
assertThat(bf.getBeanDefinition("other").isAutowireCandidate(), is(false));
Expand Down