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 ...
随机推荐
- C# BarCodeToHTML条码生成类
来自:http://www.sufeinet.com/forum.php?mod=viewthread&tid=656&extra=page%3D1%26filter%3Dtypeid ...
- Picasso 图片加载库
Picasso 英文意思国外一个很有名的画家毕加索的名字,国外项目取名还是很有意思的! 从github新下载的picasso项目有依赖其他第三方开源项目okhttp和okio,这两个项目也是相当经典的 ...
- PS 滤镜——旋转模糊
这里给出灰度图像的模糊算法,彩色图像只要分别对三个通道做模糊即可. %% spin blur % 旋转模糊 clc; clear all; close all; I=imread('4.jpg'); ...
- SharePoint 2010 -- Silverlight托管客户端模型简单示例
Silverlight托管客户端模型,是SharePoint2010推出的三种客户端模型".NET托管"."ECMAScript"."Sliverli ...
- jQuery事件控制点击内容下拉
1.设计实例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- phantomjs 爬去动态页面
最近有一个小需求,需要根据用户输入的某宝的店铺 url,检查地址是否存在,并抓取店铺名称.某宝店铺 url 的 title 通常是 xx-xx-xx 的形式,中间的 xx 就是对应的店铺名称. 这个需 ...
- form表单提交转为可被 getModel(PROJECT.class ,null);接收
var form = new mini.Form("#editForm"+id); form.validate();if (!form.isValid()) { alert('信息 ...
- java keytool
1.tomcat 配置Https,server.xml <Connector protocol="org.apache.coyote.http11.Http11Protocol&quo ...
- java死锁小例子
package cn.com.io.threadDemo.ThreadSyn; /** * 通过两个属性值创建死锁 * 本程序通过两个线程各自锁定一个属性值,这样两个线程都无法结束,造成死锁 * @a ...
- 常常搞不清楚SQLServer中的sp_columns来看一看
The sp_columns catalog stored procedure is equivalent to SQLColumns in ODBC. The results returned ar ...