使用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());
    }

原文地址:https://www.cnblogs.com/haohanwuyin/p/11756619.html

时间: 2024-08-28 19:59:45

使用SMM框架开发企业级应用-----Spring集合注入和域属性自动注入byName和byType的相关文章

使用SMM框架开发企业级应用-----Spring简介即Spring Ioc

Spring框架简介 Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅限于服务器端的开发.从简单性.可测试性和松耦合的角度而言,任何Java应用都可以从S

使用SMM框架开发企业级应用-----初始Mybatis的模糊查询以及自动映射

在学习MyBatis过程中想实现模糊查询,下面列举几种方式:1.用${…}代替#{…}    SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');  2.  bind标签  3. CONCAT Mybatis的自动映射 0x00:引子 在 MyBatis 的映射配置文件中,select 标签查询配置结果集时使用过 resultType 属性,当在 resultType 中定义一个 Java 包装类时,

使用SMM框架开发企业级应用-----面试题

Spring中的bean 组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean. 简单地讲,bean就是由IoC容器初始化.装配及管理的对象 Spring中的bean默认都是单例的(scope="singleton"默认值) 我们可以设置为多例(scope="prototype") bean的作用域 当scope="singleton"时 Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象

使用SMM框架开发企业级应用-----mybatis和spring整合

注解式 首先导入依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties>

使用SMM框架开发企业级应用-----MVC参数传递

处理乱码关于页面传值到后台和后台传值到页面,首先要解决的是中文乱码 post乱码在web.xml中加入过滤器 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <par

使用SMM框架开发企业级应用-----关联查询

关联映射一对多 以国家和省份对应的一对多关系举例. smbms_role数据库: select u.id,u.userName,u.userRole, r.rid,r.roleName from smbms_user as u,smbms_role as r where u.userRole=r.rid and r.rid=3 SmbmsRole实体类:  Dao层接口:  编写小配置xml文件: 测试: 自连接一对多 数据库: 实体类: public class Category {    

使用SMM框架开发企业级应用-----打印机案例

Ø 要求:可灵活配置使用彩色墨盒或灰色墨盒 Ø 可灵活配置打印页面的大小 实现步骤 打印机功能的实现依赖于墨盒和纸张(对象间的依赖) 定义Ink和Paper接口 使用Ink接口和Paper接口开发打印机程序 开发Ink接口和Paper接口的实现类:ColorInk,GreyInk和TextPaper 组装打印机,运行调试 测试 原文地址:https://www.cnblogs.com/haohanwuyin/p/11740381.html

使用SMM框架开发企业级应用-----基础配置

视图解析器 在spring-mvc文件中编写 <?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.

使用SMM框架开发企业级应用-----mybatis注解

@select查詢 @insert添加 @delete刪除 @update修改 @Results自关联 @Results映射 @One UserByRole表: RoleByUser表: @Many RoleByUser表: UserByRole表: 原文地址:https://www.cnblogs.com/haohanwuyin/p/11740304.html