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=""></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=""></property>
</bean>
结果依旧,正常运行
SSM-Spring-06:Spring的自动注入autowire的byName和byType的更多相关文章
- Quartz与Spring集成 Job如何自动注入Spring容器托管的对象
在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类和方法可以是普通类.很显然,第二种方式远比第一种方式来的 ...
- spring mvc:属性无法自动注入
在使用spring mvc 3开发一个项目模块时,遇到这样一个奇怪的问题: 前端页面发送的请求中,所有参数都无法自动注入到指定的@ModelAttribute对象中,经过检查,参数名称与接受对象的属性 ...
- Spring Boot @Autowired 没法自动注入的问题
Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...
- spring boot测试类自动注入service或dao
使用Spring Boot进行单元测试时,发现使用@Autowired注解的类无法自动注入,当使用这个类的实例的时候,报出NullPointerException,即空指针异常. Spring Boo ...
- web 工程中利用Spring的 ApplicationContextAware接口自动注入bean
最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApp ...
- spring的两种属性注入方式setter注入和构造器注入或者自动注入
1.这里的属性自动注入,与注解配置bean是两回事.这里的自动注入,指的是bean属性的自动注入. bean属性自动注入,包括byNAme和ByType两码事. 2.所有的applicationCon ...
- spring学习 十五 spring的自动注入
一 :在 Spring 配置文件中对象名和 ref=”id” ,id 名相同使用自动注入,可以不配置<property/>,对应的注解@Autowired的作用 二: 两种配置办法 (1 ...
- Spring框架使用@Autowired自动装配引发的讨论
问题描述 有同事在开发新功能测试时,报了个错,大致就是,在使用 @Autowired 注入时,某个类有两个bean,一个叫a,一个叫b. 一般这种情况应该声明注入哪个bean,他没有声明,他不知道这个 ...
- Spring中的注解配置-注入bean
在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...
随机推荐
- iOS关于蓝牙连接的简单介绍与使用
下面是两台iPhone6连接同一台蓝牙设备的结果: **成功连接**** peripheral: <CBPeripheral: 0x1700f4500, identifier = 50084F6 ...
- 中文注释 MariaDB my.cnf 大型服务器配置模板
文件如下所示,请根据需要进行修改: 翻译日期: 2014年5月22日 翻译人员: 铁锚 # /usr/share/mysql/my-huge.cnf # MariaDB 配置文件 示例模板(huge, ...
- 队列链式存储 - 设计与实现 - API函数
队列相关基础内容参我的博文:队列顺序存储 - 设计与实现 - API函数 队列也是一种特殊的线性表:可以用线性表链式存储来模拟队列的链式存储. 主要代码: // linkqueue.h // 队列链式 ...
- OpenGL Shader Key Points (3)
Shader和Program Program Link过后,Shader就可以从Program中Detach并删掉.这样是不是可以节省一点点显存呢? 链接到同一个program的vertex和frag ...
- Zookeeper管理多个HBase集群
zookeeper是hbase集群的"协调器".由于zookeeper的轻量级特性,因此我们可以将多个hbase集群共用一个zookeeper集群,以节约大量的服务器.多个hbas ...
- linux终端用户登录流程
终端可以通过连线,猫或网络(ssh,telnet,rlogin)和unix相连. 对于每个物理终端端口,都有一个getty监视,getty是由init程序启动的. getty收到用户名->调用l ...
- spring boot + mybatis + druid配置实践
最近开始搭建spring boot工程,将自身实践分享出来,本文将讲述spring boot + mybatis + druid的配置方案. pom.xml需要引入mybatis 启动依赖: < ...
- Javascript的console['']几种常用输入方法
1.console.log是最常用的输入方法,正常化输出语句,还具有print占位符整数(%d||%i),浮点数(%f),对象(%o),字符(%s); 2.console.error输出错误化的语句 ...
- WebRequestHelper
老是浪费时间写这个类,干脆记录在博客里: public class WebRequestHelper { #region Post public static CookieContainer GetC ...
- innobackupex 简单使用笔记
innobackupex 选项介绍 --backup 备份 --apply-log 应用日志 --move-back --copy-back 恢复 --export 只导出单个表.前提是使用in ...