SSM-Spring-06:Spring的自动注入autowire的byName和byType

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

di的注入上次讲了一些,这次主要阐述域属性的自动注入

先讲byType方式

  看名字就知道是根据类型进行自动注入

  案例:

  实体类:(俩个,一个学生类,一个汽车类)

package cn.dawn.day06autowire;

/**
 * Created by Dawn on 2018/3/5.
 */
//student类
public class Student {
    private String name;
    private Integer age;
    private Car car;

    //带参构造
    public Student(String name, Integer age, Car car) {
        this.name = name;
        this.age = age;
        this.car = car;
    }

    //无参构造
    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}

package cn.dawn.day06autowire;

/**
 * Created by Dawn on 2018/3/5.
 */
public class Car {
    private String color;
    private String type;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

  在配置文件中:

    <!--汽车的bean-->
    <bean id="car" class="cn.dawn.day06autowire.Car">
        <property name="color" value="黑色"></property>
        <property name="type" value="奥迪"></property>
    </bean>

    <!--装配student-->
    <bean id="student" class="cn.dawn.day06autowire.Student" autowire="byType">
        <property name="name" value="马云"></property>
        <property name="age" value="18"></property>
    </bean>

  单测方法:

package cn.dawn.day06autowire;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180306 {

    @Test
    /*di自动注入*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day06autowire.xml");
        Student student = (Student) context.getBean("student");
        System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType());
    }
}

  单测后可以正常执行

  但是问题来了:如果有一个类继承Car,并且在spring的配置文件中配置了他的bean,那么byType还可以吗?

    

  结果是不行的,它报错的解释是,不能自动装配,有比一个多的car类型,所以,引出了另外一种方式byName

-------------------------------------------------------------------------------------------------------------

  byName的方式,要求实体类的关联的那个对象名要和上面配置的bean的id一样,就可以自动装配,我的上面汽车的bean的id是car,说明只要Student类中的关联的Car类型的对象的名字是car就可以自动装配

  代码

    <bean id="student" class="cn.dawn.day06autowire.Student" autowire="byName">
        <property name="name" value="马云"></property>
        <property name="age" value="18"></property>
    </bean>

  结果依旧,正常运行

原文地址:https://www.cnblogs.com/DawnCHENXI/p/8516045.html

时间: 2024-08-09 21:53:30

SSM-Spring-06:Spring的自动注入autowire的byName和byType的相关文章

Quartz与Spring集成 Job如何自动注入Spring容器托管的对象

在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类和方法可以是普通类.很显然,第二种方式远比第一种方式来的灵活. 测试环境 Spring3 M2 quartz-2.1.7 我们要达到这样的效果 public class CancelUnpaidOrderTask implements Job { @Autowired private AppOrderService orderService; @Over

web 工程中利用Spring的 ApplicationContextAware接口自动注入bean

最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApplicationContext 等对象去加载Spring配置文件,这样做也是可以, 但是在加载Spring配置文件的时候,就会生成一个新的ApplicaitonContext对象而不是Spring容器帮我们生成的哪一个, 这样就产生了冗余, 所以不采用应用程序手动加载文件的方式,而是使用Applic

spring mvc:属性无法自动注入

在使用spring mvc 3开发一个项目模块时,遇到这样一个奇怪的问题: 前端页面发送的请求中,所有参数都无法自动注入到指定的@ModelAttribute对象中,经过检查,参数名称与接受对象的属性名称保持一致. 测试其它类似的模块时发现并未出现同样的情况,说明后端应该是正常的,问题出现在前端. 经检查,该模块前端使用异步(ajax)的方式传递参数,设置了request的content-type为application/json,去除这个设置后正常.

使用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(

spring的两种属性注入方式setter注入和构造器注入或者自动注入

1.这里的属性自动注入,与注解配置bean是两回事.这里的自动注入,指的是bean属性的自动注入. bean属性自动注入,包括byNAme和ByType两码事. 2.所有的applicationContext都实现了resourceLoader接口,通过resourceLoader可以获得resource实例,进而可以访问资源文件. 所以要在类中获得resource实例,必须实现applicationContextAware接口.

spring IOC控制反转 DI注入

<!-- 初始化 init-method="init" 销毁destroy-method="destory" --> <!--懒加载 lazy-init="true" --> <bean id="IUDao" class="dao.IUDao" scope="singleton" init-method="init" destroy-me

Injection of autowired dependencies failed; autowire 自动注入失败,测试类已初始化过了Spring容器。

1 严重: StandardWrapper.Throwable 2 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreatio

Spring框架使用ByName自动注入同名问题剖析

问题描述 ??我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和==ByType==这两种机制(两者的用法和区别可以参考[email protected]官方文档).但大家在使用Autowire当中==ByName==机制的时候有没有思考过这样一个问题,当我们配置了两个name属性相同的Bean,Spring在自动注入的时候会采取怎样处理方式?会覆盖?还是抛出异

Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea

文章大纲 在xml中声明bean和注入bean 在xml中声明bean和自动注入bean 自动扫描bean和自动注入bean 对自动扫描bean增加约束条件 首次接触spring请参考 Spring 3.0 学习-环境搭建和三种形式访问 1.典型的Spring XML 配置文件表头 <?xml version="1.0" encoding="UTF-8"?><!-- 一般化的Spring XML 配置 --> <beans xmlns=