Spring Framework_学习记录_03

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

}

  • Triangle.java
package org.zcs.spring;

public class Triangle {

	private String type;
	private int height;
	
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public String getType() {
		return type;
	}
	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 void setType(String type) {
		this.type = type;
	}
	public void draw() {
		System.out.println(getType()+ " : There is a triangle. " + getHeight()  );
	}
}

  • 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" >
   	 		<constructor-arg  index = "0" value = "this is three say"></constructor-arg>
   	 		<constructor-arg index = "1" value = "20"></constructor-arg>
   	</bean>
  </beans>
  • 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" >
   	 		<constructor-arg   type = "int" value = "50"></constructor-arg>
   	  
   	</bean>
  </beans>
  • 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" >
   	 		<constructor-arg  value = "this is three say"></constructor-arg>
   	 		<constructor-arg value = "20"></constructor-arg>
   	</bean>
  </beans>
2.学习笔记
  • 主要学习了如何在bean中通过构造函数传参赋值的三种方式。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值