java spring Bean Definition Inheritance 继承实例



DrawingApp.java

package zxl;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DrawingApp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
		Triangle triangle =(Triangle) context.getBean("triangle1");
		triangle.draw();
	}

}

spring.xml

<?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="parenttriangle" class="zxl.Triangle" >
        <property name="pointA" ref="pointA" />
    </bean>
    
    <bean id="triangle1"  class="zxl.Triangle" parent="parenttriangle" >	
		<property name="pointB" ref="pointB" />
		<property name="pointC" ref="pointC" />
    </bean>

    <bean id="triangle2"  class="zxl.Triangle" parent="parenttriangle" >	
		<property name="pointB" ref="pointB" />
    </bean>
    
    
    <bean id="pointA" class="zxl.Point" >
        <property name="x" value="0" />
        <property name="y" value="0" />
    </bean>

    <bean id="pointB" class="zxl.Point" >
        <property name="x" value="-20" />
        <property name="y" value="0" />
    </bean>

    <bean id="pointC" class="zxl.Point" >
        <property name="x" value="20" />
        <property name="y" value="0" />
    </bean>

</beans>

Triangle.java

package zxl;

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 =null;
	
	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("point A ("+getPointA().getX()+","+getPointA().getY()+")");
		System.out.println("point B ("+getPointB().getX()+","+getPointB().getY()+")");
		System.out.println("point C ("+getPointC().getX()+","+getPointC().getY()+")");
		
	}

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		// TODO Auto-generated method stub
		this.context = context;
	}

	@Override
	public void setBeanName(String beanName) {
		// TODO Auto-generated method stub
		System.out.println("Bean name is : "+beanName);
	}
}

Point.java

package zxl;

public class Point {
	private int x;
	private int 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;
	}
	
}


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值