• spring框架学习笔记(四)


    自动装配

    通过id名字进行自动装配

    新建测试用的两个bean类:ClassRoom、Teacher

    代码如下:

     Teacher类:

    public class Teacher {
    	private Integer teacherNo;
    	private String name;
    	/**
    	 * 
    	 * @return the teacherNo
    	 */
    	public Integer getTeacherNo() {
    		return teacherNo;
    	}
    	/**
    	 * @param teacherNo the teacherNo to set
    	 */
    	public void setTeacherNo(Integer teacherNo) {
    		this.teacherNo = teacherNo;
    	}
    	/**
    	 * 
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    	/**
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    	/* (non-Javadoc)
    	 * @see java.lang.Object#toString()
    	 */
    	@Override
    	public String toString() {
    		return "Teacher [teacherNo=" + teacherNo + ", name=" + name + "]";
    	}
    

     ClassRoom类:

    public class ClassRoom {
    	private String className;
    	private String classNo;
    	private Teacher teacher;
    	/**
    	 * 
    	 * @return the className
    	 */
    	public String getClassName() {
    		return className;
    	}
    	/**
    	 * @param className the className to set
    	 */
    	public void setClassName(String className) {
    		this.className = className;
    	}
    	/**
    	 * 
    	 * @return the classNo
    	 */
    	public String getClassNo() {
    		return classNo;
    	}
    	/**
    	 * @param classNo the classNo to set
    	 */
    	public void setClassNo(String classNo) {
    		this.classNo = classNo;
    	}
    	/**
    	 * 
    	 * @return the teacher
    	 */
    	public Teacher getTeacher() {
    		return teacher;
    	}
    	/**
    	 * @param teacher the teacher to set
    	 */
    	public void setTeacher(Teacher teacher) {
    		this.teacher = teacher;
    	}
    	/* (non-Javadoc)
    	 * @see java.lang.Object#toString()
    	 */
    	@Override
    	public String toString() {
    		return "Class [className=" + className + ", classNo=" + classNo
    				+ ", teacher=" + teacher + "]";
    	}
    	
    	
    }
    

     配置文件如下:

    <?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="teacher" class="com.pfSoft.autowire.beans.Teacher" p:teacherNo="89757" p:name="L林俊杰"></bean>
    	<bean id="classRoomBean" class="com.pfSoft.autowire.beans.ClassRoom" p:classNo="302" p:className="三年二班" autowire="byName"></bean>
    </beans>
    

     关键点在于,autowire选择的是byName。同时id为“teacher”的bean,与ClassRoom类中seeter风格的属性名具有相同的名字。因此实现了通过名字进行自动装配。

    同样的autowire="byType"是表示可以通过类型来进行自动匹配,不过需要注意的是如果同类型有多个bean的话就会报不只一个的异常了。

  • 相关阅读:
    超链接标签、链接地址、锚文本及图片标签
    有序无序列表,div盛放逻辑版块,table表格
    函数的默认值与动态参数arguments的总结
    浏览器中常见的html语义化标签
    html基本介绍,了解html与css,html语法和结构
    js函数与作用域,了解函数基本概念
    JavaScrip流程控制之switch选择,for循环
    JavaScript之if流程控制演练,if写在区间内怎么解决
    JavaScript数据类型typeof()和转换
    C++走向远洋——60(十四周阅读程序、STL中的简单容器和迭代器)
  • 原文地址:https://www.cnblogs.com/falcon-fei/p/5419278.html
Copyright © 2020-2023  润新知