When we set the autowire property by "autodetect" than it autowired by constructor first then using by Type and byName. if any exception occurs handle the same way as with constructor and byType wiring.
Let see example-
Directory structure-
class Draw.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 id="point1" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="draw" class="com.test.Draw">
</bean>
<bean id="triangle" class="com.test.Triangle" autowire="autodetect">
</bean>
</beans>
output is-
Let see example-
Directory structure-
class Draw.java
package com.test;
public class Draw {
public void draw()
{
System.out.println("Triangle Drawn");
}
}
class Point.java
public class Draw {
public void draw()
{
System.out.println("Triangle Drawn");
}
}
class Point.java
package com.test;
public class Point {
private int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
class Triangle.javapublic class Point {
private int x,y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package com.test;
public class Triangle {
private Point point1;
private Draw dr;
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public void show()
{
System.out.println("Ponit1 x="+point1.getX()+"y="+point1.getY());
dr.draw();
}
public Draw getDr() {
return dr;
}
public void setDr(Draw dr) {
this.dr = dr;
}
}
class Main.javapublic class Triangle {
private Point point1;
private Draw dr;
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public void show()
{
System.out.println("Ponit1 x="+point1.getX()+"y="+point1.getY());
dr.draw();
}
public Draw getDr() {
return dr;
}
public void setDr(Draw dr) {
this.dr = dr;
}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.show();
}
}
spring.xmlimport org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("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 id="point1" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="draw" class="com.test.Draw">
</bean>
<bean id="triangle" class="com.test.Triangle" autowire="autodetect">
</bean>
</beans>
Ponit1 x=10y=20
Triangle Drawn
Triangle Drawn
For Further Reading,
0 comments:
Post a Comment