zoukankan      html  css  js  c++  java
  • 使用SMM框架开发企业级应用-----Spring集合注入和域属性自动注入byName和byType

    Spring集合的注入

      步骤一:导入依赖

     步骤二:创建实体类

     步骤三:创建大配置文件

     步骤四:测试

    域属性自动注入

      byName与byType

      步骤一:创建两个实体类

    public class Student {
        private Integer stuid;
        private String stuName;
        private Teacher teacher;
    
        public Teacher getTeacher() {
            return teacher;
        }
    
        public void setTeacher(Teacher teacher) {
            this.teacher = teacher;
        }
    
        public Integer getStuid() {
            return stuid;
        }
    
        public void setStuid(Integer stuid) {
            this.stuid = stuid;
        }
    
        public String getStuName() {
            return stuName;
        }
    
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
    }
    public class Teacher {
        private Integer tid;
        private String tname;
    
        public Integer getTid() {
            return tid;
        }
    
        public void setTid(Integer tid) {
            this.tid = tid;
        }
    
        public String getTname() {
            return tname;
        }
    
        public void setTname(String tname) {
            this.tname = tname;
        }
    }

     步骤二:创建大配置文件(在bean节点中增加autowire属性,设值为byType)

      要求:给Teacher赋值的bean节点的id与域属性的名字相同

    复制代码
    <?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:aop="http://www.springframework.org/schema/aop"
           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
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byName">
            <property name="stuid" value="14564"></property>
            <property name="stuName" value="陈浩"></property>
        </bean>
        <bean id="teacher" class="com.SpringMckz.entity.Teacher">
            <property name="tid" value="74894790"></property>
            <property name="tname" value="张三"></property>
        </bean>
    </beans>

      步骤四:测试(autowire="byName")

    @Test
        public void testsentitu(){
            ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student sss = (Student)atc.getBean("stuentity");
            System.out.println(sss.getTeacher().getTid());
            System.out.println(sss.getTeacher().getTname());
        }

       步骤五:测试(autowire="byType")

      要求:保证域属性的类型与bean的类型一致,与其相兼容的类型也不可以

    复制代码
    <?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:aop="http://www.springframework.org/schema/aop"
           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
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="stuentity" class="com.SpringMckz.entity.Student" autowire="byType">
            <property name="stuid" value="154847"></property>
            <property name="stuName" value="陈浩"></property>
        </bean>
        <bean id="teacher" class="com.SpringMckz.entity.Teacher">
            <property name="tid" value="484"></property>
            <property name="tname" value="沉默"></property>
        </bean>
    </beans>
    @Test
        public void testsentitu(){
            ApplicationContext atc=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student sss = (Student)atc.getBean("stuentity");
            System.out.println(sss.getTeacher().getTid());
            System.out.println(sss.getTeacher().getTname());
        }
     
  • 相关阅读:
    一题多解 —— 同时找到序列的最大值最小值
    中位数与顺序统计量
    软件开发 —— 极限编程(XP:Extreme Programming)
    一题多解 —— 二项式分布的期望和方差的计算
    C++中explicit关键字用法
    【联系】二项分布的对数似然函数与交叉熵(cross entropy)损失函数
    随机变量统计独立性的相关证明
    PowerShell管理SCOM_批量设置维护模式(下)
    导出AD用户所属组,查询AD用户(aduser)
    SQL脚本运行
  • 原文地址:https://www.cnblogs.com/haohanwuyin/p/11756619.html
Copyright © 2011-2022 走看看