In the spring bean configuration file both id and name attributes does not allow duplicate values (we must
supply unique values).
"id" will not allow special characters like "/ , " whereas "name" attribute will allow special characters like "/ , space". By using "name" attribute we can assign multiple names to one spring bean configuration file. For example:
"id" will not allow special characters like "/ , " whereas "name" attribute will allow special characters like "/ , space". By using "name" attribute we can assign multiple names to one spring bean configuration file. For example:
<bean name="addr1, addr2" class="com.ram.beans.Address">
To supply multiple spring bean configuration files at the time of creating spring container object we must supply
configuration file names as shown below.
ApplicationContext container = new ClassPathXmlApplicationContext("spring.xml", "another.xml");
The advantage of multiple container is maintenance becomes easy. Below is an example of multiple containers.
One of the container can be considered as parent and another as child.
First create a Address bean under src as shown below.
File: com.ram.beans.Address.java
package com.ram.beans; public class Address { private String street; private String city; private String state; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
First create a Person bean under src as shown below.
File: com.ram.beans.Person.java
package com.ram.beans; public class Person { private int pid; private String name; private Address address; public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Next create applicationContext.xml file under src. The file is given below.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="addr" class="com.ram.beans.Address"> <property name="street" value="Richmond"></property> <property name="city" value="Melbourne"></property> <property name="state" value="Victoria"></property> </bean> </beans>
Next create anotherApplicationContext.xml file under src. The file is given below.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="person" class="com.ram.beans.Person"> <property name="pid" value="1"></property> <property name="name" value="Ram"></property> <property name="address"> <ref parent="addr"/> </property> </bean> </beans>
Next create a main class under src as shown.
File: com.ram.app.TwoAppContextExample.java
package com.ram.app; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ram.beans.Person; public class TwoAppContextExample { public static void main(String[] args) { ApplicationContext parentContainer = new ClassPathXmlApplicationContext("applicationContext.xml"); ApplicationContext childContainer = new ClassPathXmlApplicationContext(new String[]{"anotherApplicationContext.xml"}, parentContainer); Person person = childContainer.getBean("person", Person.class); System.out.println(person.getPid()); System.out.println(person.getName()); System.out.println(person.getAddress().getStreet()); System.out.println(person.getAddress().getCity()); System.out.println(person.getAddress().getState()); } }
In above xml anotherApplicationContext.xml file, for the "ref" tag we have used an attribute "parent".
We have three attributes for reference type. They are:
bean: When we specify <ref bean="addr"/> it checks whether the corresponding bean id is available in the current configuration file. If not available it will check in imported configuration file.
local: When we specify <ref local="addr"/> the xpring container checks only in the current spring bean configuration file.
parent: When we specify <ref parent="addr"/> the spring container checks only in the parent spring bean configuration file.
- bean
- local
- parent
bean: When we specify <ref bean="addr"/> it checks whether the corresponding bean id is available in the current configuration file. If not available it will check in imported configuration file.
local: When we specify <ref local="addr"/> the xpring container checks only in the current spring bean configuration file.
parent: When we specify <ref parent="addr"/> the spring container checks only in the parent spring bean configuration file.
No comments:
Post a Comment