版权声明: https://blog.csdn.net/qq_38386316/article/details/88605661
1.在bean内部注入
- DrawingApp.java
package org.zcs.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
//BeanFactory factory = new FileSystemXmlApplicationContext("src/spring.xml");
//Triangle triangle = new Triangle();
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle)context.getBean("triangle");
triangle.draw();
}
}
- Triangle.java
package org.zcs.spring;
public class Triangle {
private Point pointA;
private Point pointB;
private Point pointC;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
private String test;
public Point getPointA() {
return pointA;
}
public void setPointA(Point pointA) {
this.pointA = pointA;
}
public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
public Point getPointC() {
return pointC;
}
public void setPointC(Point pointC) {
this.pointC = pointC;
}
public void draw () {
System.out.println("This is Point A: (" +getPointA().getX() +"," + (getPointA().getY()) +")");
System.out.println("This is Point B: (" +getPointB().getX() +"," + (getPointB().getY()) +")");
System.out.println("This is Point C: (" +getPointC().getX() +"," + (getPointC().getY()) +")");
System.out.println(this.test);
}
}
- spring.xml
<?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.xsd">
<bean id = "triangle" class= "org.zcs.spring.Triangle" name = "triangle-name">
<property name="pointA" ref = "zeroPoint" >
</property>
<property name="test" >
<idref bean = "zeroPoint"/>
</property>
<property name="pointB" >
<bean id= "twoPoint" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property>
</bean>
</property>
<property name="pointC" >
<bean id= "threePoint" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property>
</bean>
</property>
</bean>
<bean id= "zeroPoint" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "0"></property>
</bean>
<alias name="triangle-name" alias="triangle-alias"/>
</beans>
2.学习笔记
- 本次学习了在Beans内部注入对象实例。
- 学习了如何定义别名。
- 认识了idref与ref的区别。
3.问题记录
- idref注入的只是目标bean的id,而不是目标bean的实例,同时使用idref容器在部署的时候还会验证这个名称是否存在。
其实idref就跟value一样,只是将某个字符串注入到属性或者构造函数中,只不过注入的是某个bean定义的id属性值。 - 但是ref则不同,ref元素是将目标bean定义的实例注入到属性或者构造函数中,