Skip to content

Commit c7a350c

Browse files
committed
SPR-7477 - Added lazy-init attribute to jee namespace
1 parent 385d8e9 commit c7a350c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

org.springframework.context/src/main/java/org/springframework/ejb/config/AbstractJndiLocatingBeanDefinitionParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ejb.config;
1818

1919
import org.w3c.dom.Element;
20+
import static org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.*;
2021

2122
import org.springframework.beans.factory.config.RuntimeBeanReference;
2223
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -31,6 +32,7 @@
3132
*
3233
* @author Rob Harrop
3334
* @author Juergen Hoeller
35+
* @author Oliver Gierke
3436
* @since 2.0
3537
*/
3638
abstract class AbstractJndiLocatingBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
@@ -44,7 +46,8 @@ abstract class AbstractJndiLocatingBeanDefinitionParser extends AbstractSimpleBe
4446

4547
@Override
4648
protected boolean isEligibleAttribute(String attributeName) {
47-
return (super.isEligibleAttribute(attributeName) && !ENVIRONMENT_REF.equals(attributeName));
49+
return (super.isEligibleAttribute(attributeName) && !ENVIRONMENT_REF.equals(attributeName) && !LAZY_INIT_ATTRIBUTE
50+
.equals(attributeName));
4851
}
4952

5053
@Override
@@ -61,6 +64,10 @@ protected void postProcess(BeanDefinitionBuilder definitionBuilder, Element elem
6164
definitionBuilder.addPropertyValue(JNDI_ENVIRONMENT, new RuntimeBeanReference(envRef));
6265
}
6366
}
64-
}
6567

68+
String lazyInit = element.getAttribute(LAZY_INIT_ATTRIBUTE);
69+
if (StringUtils.hasText(lazyInit) && !DEFAULT_VALUE.equals(lazyInit)) {
70+
definitionBuilder.setLazyInit(TRUE_VALUE.equals(lazyInit));
71+
}
72+
}
6673
}

org.springframework.context/src/main/resources/org/springframework/ejb/config/spring-jee-3.1.xsd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@
205205
]]></xsd:documentation>
206206
</xsd:annotation>
207207
</xsd:attribute>
208+
<xsd:attribute name="lazy-init" default="default" type="beans:defaultable-boolean">
209+
<xsd:annotation>
210+
<xsd:documentation><![CDATA[
211+
Indicates whether or not this bean is to be lazily initialized.
212+
If false, it will be instantiated on startup by bean factories
213+
that perform eager initialization of singletons. The default is
214+
"false".
215+
216+
Note: This attribute will not be inherited by child bean definitions.
217+
Hence, it needs to be specified per concrete bean definition.
218+
]]></xsd:documentation>
219+
</xsd:annotation>
220+
</xsd:attribute>
208221
</xsd:extension>
209222
</xsd:complexContent>
210223
</xsd:complexType>

org.springframework.context/src/test/java/org/springframework/ejb/config/JeeNamespaceHandlerTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* @author Rob Harrop
3636
* @author Juergen Hoeller
3737
* @author Chris Beams
38+
* @author Oliver Gierke
3839
*/
3940
public class JeeNamespaceHandlerTests {
4041

@@ -129,6 +130,16 @@ public void testComplexRemoteSlsb() throws Exception {
129130
assertPropertyValue(beanDefinition, "cacheSessionBean", "true");
130131
}
131132

133+
@Test
134+
public void testLazyInitJndiLookup() throws Exception {
135+
BeanDefinition definition = this.beanFactory.getMergedBeanDefinition("lazyDataSource");
136+
assertTrue(definition.isLazyInit());
137+
definition = this.beanFactory.getMergedBeanDefinition("lazyLocalBean");
138+
assertTrue(definition.isLazyInit());
139+
definition = this.beanFactory.getMergedBeanDefinition("lazyRemoteBean");
140+
assertTrue(definition.isLazyInit());
141+
}
142+
132143
private void assertPropertyValue(BeanDefinition beanDefinition, String propertyName, Object expectedValue) {
133144
assertEquals("Property '" + propertyName + "' incorrect",
134145
expectedValue, beanDefinition.getPropertyValues().getPropertyValue(propertyName).getValue());

org.springframework.context/src/test/java/org/springframework/ejb/config/jeeNamespaceHandlerTests.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xmlns:util="http://www.springframework.org/schema/util"
55
xmlns:jee="http://www.springframework.org/schema/jee"
6-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
8-
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
7+
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
8+
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"
99
default-lazy-init="true">
1010

1111
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
@@ -67,5 +67,12 @@
6767
cache-session-bean="true">
6868
<jee:environment>foo=bar</jee:environment>
6969
</jee:remote-slsb>
70+
71+
<!-- Lazy beans Tests-->
72+
<jee:jndi-lookup id="lazyDataSource" jndi-name="jdbc/MyDataSource" lazy-init="true" />
73+
<jee:local-slsb id="lazyLocalBean" jndi-name="ejb/MyLocalBean"
74+
business-interface="org.springframework.beans.ITestBean" lazy-init="true" />
75+
<jee:remote-slsb id="lazyRemoteBean" jndi-name="ejb/MyRemoteBean"
76+
business-interface="org.springframework.beans.ITestBean" lazy-init="true" />
7077

7178
</beans>

0 commit comments

Comments
 (0)