Normally we injected the beans in xml file ,than the xml loads in application.But here another new feature of spring framework is that we can inject the bean property outside the xml and before the load xml in application.
To more understand it let illustrate below example-
Directory structure of project-
Class Point.java
Class Main.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="point" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="triangle" class="com.test.Triangle" abstract="true">
<property name="p" ref="point">
</property>
</bean>
</beans>
output is-
triangle bean definition from the XML file to the tri instance.
To more understand it let illustrate below example-
Directory structure of project-
Class Point.java
package com.test;
public class Point {
private int x,y;
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
Class Triangle.javapublic class Point {
private int x,y;
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
}
package com.test;
public class Triangle {
private String name;
private Point p;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public void show()
{
System.out.println("circle type is="+name);
System.out.println("x="+p.getX()+"\n y="+p.getY());
}
}
public class Triangle {
private String name;
private Point p;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point getP() {
return p;
}
public void setP(Point p) {
this.p = p;
}
public void show()
{
System.out.println("circle type is="+name);
System.out.println("x="+p.getX()+"\n y="+p.getY());
}
}
Class Main.java
package com.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String args[]) {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
Triangle tri=new Triangle();
tri.setName("equ");
((AutowireCapableBeanFactory)beanFactory).applyBeanPropertyValues
(tri, "triangle");
tri.show();
}
}
spring.xmlimport org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String args[]) {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
Triangle tri=new Triangle();
tri.setName("equ");
((AutowireCapableBeanFactory)beanFactory).applyBeanPropertyValues
(tri, "triangle");
tri.show();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean name="point" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="triangle" class="com.test.Triangle" abstract="true">
<property name="p" ref="point">
</property>
</bean>
</beans>
output is-
circle type is=equ
x=10
y=20
Here the applyBeanPropertyValues() method on AutowireCapableBeanFactory to apply a bean definition’s properties to an existing bean. We are linking thex=10
y=20
triangle bean definition from the XML file to the tri instance.
For Further Reading,
0 comments:
Post a Comment