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());
        }
     
  • 相关阅读:
    NET基础课--NET的一些概念0
    项目异常及处理方法记录
    个人总结
    NuGet学习笔记(4)—— 实践
    NuGet学习笔记(3)——搭建属于自己的NuGet服务器(转)
    NuGet学习笔记(2)——使用图形化界面打包自己的类库(转)
    NuGet学习笔记(1)——初识NuGet及快速安装使用(转)
    关系数据库SQL之基本数据查询:子查询、分组查询、模糊查询
    <iOS>UIImage变为NSData并进行压缩
    iOS 将NSArray、NSDictionary转换为JSON格式进行网络传输
  • 原文地址:https://www.cnblogs.com/haohanwuyin/p/11756619.html
Copyright © 2011-2022 走看看