P name space.
P name space is used to define more values for the bean.
Using Properties
<bean id="configBean" class="com.searchendeca.application.PropertiesSample">
<property name="properties">
<props>
<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
Using <util:properties
<bean id="configBean" class="com.searchendeca.application.PropertiesSample">
<property name="properties" ref="props" />
</bean>
<util:properties id="props">
<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
</util:properties>
package com.searchendeca.application;
import java.util.Properties;
public class PropertiesSample {
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
public Properties getProperties() {
return properties;
}
}
Defining Null Value
<bean class="ExampleBean">
<property name="email">
<null/>
</property>
</bean>
c:nameSpace.
The c: namespace uses the same conventions as the p: one (trailing -ref for bean references) for
setting the constructor arguments by their names.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bar" class="x.y.Bar"/>
<bean id="baz" class="x.y.Baz"/>
<!-- traditional declaration -->
<bean id="foo" class="x.y.Foo">
<constructor-arg ref="bar"/>
<constructor-arg ref="baz"/>
<constructor-arg value="foo@bar.com"/>
</bean>
<!-- c-namespace declaration -->
<bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com"/>
</beans>
Compound property names
<bean id="Compound" class="com.searchendeca.employee">
<property name="employee.dept.id" value="123" />
</bean>
Using depends on
The depends-on attribute can explicitly force one
or more beans to be initialized before the bean using this element is initialized. The following example
uses the depends-on attribute to express a dependency on a single bean:
<bean id="Helloworld1" class="com.searchendeca.Helloworld1" depends-on="Helloworld"/>
<bean id="Helloworld2" class="com.searchendeca.Helloworld2" />
Lazy-initialized beans
By default, ApplicationContext implementations eagerly create and configure all singleton beans
as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the
configuration or surrounding environment are discovered immediately, as opposed to hours or even
days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
<bean id="helloWorldService2"
class="com.searchendeca.application.HelloWorld2" depends-on="configBean" lazy-init="true">
No comments:
Write comments