版权声明: https://blog.csdn.net/qq_38386316/article/details/88691926
1.回调函数:
- 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("triangle2");
triangle.draw();
}
}
- Triangle.java
package org.zcs.spring;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Triangle implements ApplicationContextAware, BeanNameAware{
private Point pointA;
private Point pointB;
private Point pointC;
private ApplicationContext context;
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 PointA: (" +getPointA().getX() +"," + getPointA().getY() +")");
System.out.println("This is PointB: (" +getPointB().getX() +"," + getPointB().getY() +")");
System.out.println("This is PointC: (" +getPointC().getX() +"," + getPointC().getY() +")");
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
// TODO Auto-generated method stub
this.context = context;
//System.out.println(context);
}
@Override
public void setBeanName(String beanName) {
// TODO Auto-generated method stub
System.out.println("The bean Name is:" + beanName);
}
}
- 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 = "parenttriangle" class = "org.zcs.spring.Triangle">
<property name="pointA" ref = "pointA"></property>
</bean>
<bean id = "triangle1" class= "org.zcs.spring.Triangle" parent = "parenttriangle" >
<property name="pointB" ref = "pointB"></property>
<property name="pointC" ref = "pointC"></property>
</bean>
<bean id = "triangle2" class= "org.zcs.spring.Triangle" parent = "parenttriangle" >
<property name="pointB" ref = "pointB"></property>
</bean>
<bean id= "pointA" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "0"></property>
</bean>
<bean id= "pointB" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property>
</bean>
<bean id= "pointC" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property>
</bean>
</beans>
2.Bean Definition Inheritance
- 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("triangle1");
triangle.draw();
}
}
- Triangle.java
package org.zcs.spring;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Triangle {
private List<Point> points;
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
}
public void draw () {
for(Point p : points) {
System.out.println("This is PointA: (" +p.getX() +"," + p.getY() +")");
}
}
}
- 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 = "parenttriangle" class = "org.zcs.spring.Triangle" abstract = "true">
<property name="points" >
<list>
<ref bean = "pointA"/>
</list>
</property>
</bean>
<bean id = "triangle1" class= "org.zcs.spring.Triangle" parent = "parenttriangle" >
<property name="points" >
<list merge = "true">
<ref bean = "pointB"/>
<ref bean = "pointC"/>
</list>
</property>
</bean>
<bean id= "pointA" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "0"></property>
</bean>
<bean id= "pointB" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property>
</bean>
<bean id= "pointC" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property>
</bean>
</beans>
3. 学习笔记
- 主要学习了回调函数的使用,显示bean的名字和内容。
- bean里面定义的继承如何使用的。
- 学习了集合的内容如何在bean中定义的。