现在的位置: 首页 > 综合 > 正文

java spring ApplicationContext-属性初始化

2013年09月20日 ⁄ 综合 ⁄ 共 3372字 ⁄ 字号 评论关闭

首先了解一下 我的上一篇博文  Eclipse+Spring学习(一)环境搭建

属性初始化

新建一个Java Project项目,添加Spring框架包:

1.在src文件中新建一个包zxl

2.添加两个类DrawingApp.java  Triangle.java

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("triangle");
		triangle.draw();
	}

}

Triangle.java

package zxl;

public class Triangle {
	
	private String type;
	
	public String getType(){
		return type;
	}
	public void setType(String type){
		this.type=type;
	}
	public void draw(){
		System.out.println(getType()+" Triangle draw");
	}
}

3.在src文件夹中创建一个xml文件 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="triangle" class="zxl.Triangle" >
        <property name="type" value="Equilateral" />
    </bean>
</beans>

使用构造函数注入pt1

Triangle.java

package zxl;

public class Triangle {
	
	private String type;
	
	public Triangle(String type){
		this.type=type;
	}
	
	public String getType(){
		return type;
	}
	/*public void setType(String type){
		this.type=type;
	}*/
	public void draw(){
		System.out.println(getType()+" 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="triangle" class="zxl.Triangle" >
        <constructor-arg value="Equilateral" />
    </bean>
</beans>

使用构造函数注入pt2

Triangle.java

package zxl;

public class Triangle {
	
	private String type;
	private int height;
	
	public int getHeight(){
		return height;
	}
	
	
	public Triangle(String type,int height){
		this.type=type;
		this.height=height;
	}
	
	public String getType(){
		return type;
	}
	/*public void setType(String type){
		this.type=type;
	}*/
	public void draw(){
		System.out.println(getType()+" Triangle draw "+getHeight());
	}
}

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="triangle" class="zxl.Triangle" >
        <constructor-arg value="Equilateral" />
    	<constructor-arg value="20" />
    </bean>
</beans>

使用构造函数注入pt3

Triangle.java

package zxl;

public class Triangle {
	
	private String type;
	private int height;
	
	public int getHeight(){
		return height;
	}
	
	public Triangle(String type){
		this.type=type;
	}
	
	public Triangle(int height){
		this.height=height;
	}
	
	public Triangle(String type,int height){
		this.type=type;
		this.height=height;
	}
	
	public String getType(){
		return type;
	}

	public void draw(){
		System.out.println(getType()+" Triangle draw "+getHeight());
	}
}

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="triangle" class="zxl.Triangle" >
    	<constructor-arg type="int" value="20" />
    </bean>
</beans>

使用构造函数注入pt4

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="triangle" class="zxl.Triangle" >
    	<constructor-arg type="java.lang.String" value="20" />
    </bean>
</beans>

使用构造函数注入pt4

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="triangle" class="zxl.Triangle" >
    	<constructor-arg index = "0" value="hello" />
    	<constructor-arg index = "1" value="20" />
    </bean>
</beans>

抱歉!评论已关闭.