zoukankan      html  css  js  c++  java
  • Spring入门-----------------属性注入和对象注入

    属性注入即通过setter方法注入bean的属性或依赖对象。

    属性注入使用<property>元素,使用name属性指定bean的属性的名称,value属性或<value>子节点指定属性值。

    原理:通过java的反射机制,调用此属性的setter方法。所以改属性必须有setter方法才能使用。

    构造器注入:

    通过构造方法注入bean的属性值或依赖的对象,它保证了bean实例在实例化后就可以使用。

    构造器注入在<constructor-arg>初始化属性值,name属性表示通过参数名称设置属性。

    构造器注入有两种方法:

    1. 按索引匹配入参

    通过参数的顺序进行初始化

    1. 按类型匹配入参

    通过方法的参数类型定位是用哪个构造方法

    1.创建一个Student类

    package com.xt.spring.student;
    
    public class Student {
    
        private String stuNo;
        private String stuName;
        public School school;
        public School getSchool() {
            return school;
        }
        public void setSchool(School school) {
            this.school = school;
        }
        public  Student(String stuNo,String stuName){
            this.stuNo=stuNo;
            this.stuName=stuName;
        }
        public Student(){
            
        }
        public String getStuNo() {
            return stuNo;
        }
        public void setStuNo(String stuNo) {
            this.stuNo = stuNo;
        }
        public String getStuName() {
            return stuName;
        }
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
        @Override
        public String toString() {
            return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", school=" + school + "]";
        }
    }

     新建一个school类,作为引用对象

    package com.xt.spring.student;
    
    public class School {
     private String address;
     private String detail;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getDetail() {
        return detail;
    }
    public void setDetail(String detail) {
        this.detail = detail;
    }
    @Override
    public String toString() {
        return "School [address=" + address + ", detail=" + detail + "]";
    }
     
    }

    2.创建配置文件applicationContext-stu.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                   http://www.springframework.org/schema/beans/spring-beans.xsd
                   http://www.springframework.org/schema/context
                   http://www.springframework.org/schema/context/spring-context.xsd
                   http://www.springframework.org/schema/util
                   http://www.springframework.org/schema/util/spring-util.xsd ">

    <!--
       DI:依赖注入
      javabean通过标签property向实体中的属性注入值。属性必须有setter方法,不然会有异常。
          原理:通过java的反射机制,调用此属性的setter方法
          通过构造方法实例化bean对象,并初始化属性值:
          通过标签constructor-arg 初始化属性值,name属性表示通过参数名称设置属性
      <bean id="stu" class="com.xt.spring.student.Student">
      <constructor-arg type="java.lang.String" value="20162430730"></constructor-arg>
      <constructor-arg type="java.lang.String" value="杨贺龙"></constructor-arg>
      </bean>
         痛过设置参数的书序进行初始化,属性index表示参数顺序

              <bean id="stu" class="spring.ioc.test.stu.Student">
              <constructor-arg value="20162430730" index="0"></constructor-arg>
              <constructor-arg value="杨贺龙" index="1"></constructor-arg>
              </bean>

    
          通过方法的参数类型定位使用哪个构造方法
          <constructor-arg type="java.lang.String" value="杨贺龙"></constructor-arg>
     向javabean属性注入一个引用:
            通过bean标签的子标签property标签向javabean属性注入一个引用,
            属性name表示bean的属性名称,ref表示注入的引用bean Name或者bean ID;
        
            在使用<property>标签向bean中注入值时,如果注入的值包含特殊符号需要使用<![CDATA[值]]以 value标签的形式注入。
       引用对象时使用ref属性,对应取值为引用对象的id
     -->

    <bean id="school" class="com.xt.spring.student.School">
    <property name="address" value="郑州大学"></property>
    <property name="detail" value="信息工程学院"></property>
    </bean>

    <bean id="stu" class="com.xt.spring.student.Student">

    <property name="stuNo" value="20162430730"></property>

    <property name="stuName" value="杨贺龙"></property>

    <property name="school" ref="school"></property>

    </bean>

    </beans>

    3.pom.xml文件与上个博客中的相同

    4.实现类

    package com.xt.spring.student;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class IOCTest {
        private ApplicationContext ioc;
    @Before
    public void Init(){
        ioc = new ClassPathXmlApplicationContext("Spring/ApplicationContext-stu.xml");
    }
    @Test
    public void Test(){
        Student stu = ioc.getBean("stu",Student.class);
        System.out.println("==========="+stu);    
    }
    }
    你一定会喜欢那个因为喜欢她而发光的自己!
    个人博客:http://www.yanghelong.top
  • 相关阅读:
    fcc的高级算法题
    jq on方法绑定多个事件
    高效的jQuery代码编写技巧
    HTML 提高页面加载速度的方法
    link 和 import 导入外部样式的区别
    js将多个方法添加到window对象上的多种方法
    js 数组删去重复的加上没有的元素
    JS中定义对象原型的两种使用方法
    CSS 清除默认样式
    JavaScript中定义对象的四种方式 2012-5-10 15:19 阅读(0)
  • 原文地址:https://www.cnblogs.com/zzu-general/p/8732615.html
Copyright © 2011-2022 走看看