Spring4自动装配(default-autowire) (转)
原文地址:http://blog.csdn.net/conglinyu/article/details/63684957
Spring 自动装配
通过配置default-autowire 属性,Spring IOC 容器可以自动为程序注入bean;默认是no,不启用自动装配;
default-autowire 的类型有byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
【byName】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byName">
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <!-- 自动装配 byName 来找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奥拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
实体类
- public class Car {
- private int id;
- private String carName;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getCarName() {
- return carName;
- }
- public void setCarName(String carName) {
- this.carName = carName;
- }
- }
- public class Employee {
- private int id;
- private String name;
- private String sex;
- private Car car;
- public Employee() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Employee(int id, String name, String sex) {
- super();
- this.id = id;
- this.name = name;
- this.sex = sex;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public Car getCar() {
- return car;
- }
- public void setCar(Car car) {
- this.car = car;
- }
- }
测试:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李经理");//在xml中属性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
【byType】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byType">
- <!-- 自动装配 byType 有两个相同类型会报错 -->
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <!-- 自动装配 byType 来找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奥拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
测试:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李经理");//在xml中属性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
运行:
【default-autowire="constructor"】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="constructor">
- <!-- 自动装配 constructor 需要写单参构造方法 -->
- <bean id="car3" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奥迪"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="贾经理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
- 自动装配 constructor 需要写单参构造方法
不写的话会报告错误
Spring4自动装配(default-autowire) (转)的更多相关文章
- Spring4自动装配(default-autowire)
§1 什么是自动装配? Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自动让Spring通过检查BeanFactory中的内容,来替我 ...
- Spring笔记04(DI(给属性赋值),自动装配(autowire))
给不同数据类型注入值: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- [转载]Spring Autowire自动装配介绍
转自: http://www.cnblogs.com/zhishan/p/3190757.html 在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型 ...
- Spring Autowire自动装配介绍
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
- Spring Autowire自动装配
在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象.但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的< ...
- Spring学习记录(三)---bean自动装配autowire
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...
- Spring 自动装配及其注解
一.属性自动装配 首先,准备三个类,分别是User,Cat,Dog.其中User属性拥有Cat和Dog对象. package com.hdu.autowire; public class User { ...
- spring的自动装配(default-autowire="byName")
自动装配,官方给出的定义是这样:Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自 动让Spring通过检查BeanFactory中的内 ...
- Spring(三)之自动装配、表达式
自动装配 自动装配(autowire)协作者 Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系.因此,如果可能的话,可以自动让Spring通过检查BeanFact ...
随机推荐
- C++编译错误C2365
曾经我们说重定义一般是函数或者变量的重定义.今天遇到了一个新类型的重定义errorC2365 #include <iostream> using namespace std; class ...
- Linux mail发送邮件
发送前一天的监控日志#!/bin/bash source /etc/profile time=`date -d '-1 day' "+%Y%m%d"` #判断服务是否已经启动 se ...
- python 微信跳一跳进阶
上一篇是通过图片识别来计算跳的距离,再计算按压时间,最后通过adb来控制手机跳的 本篇讲的是通过机器学习,来训练的算法进行跳一跳的 链接: github:https://github.com/Prin ...
- 【BZOJ3333】排队计划 树状数组+线段树
[BZOJ3333]排队计划 Description Input Output Sample Input 6 2 160 163 164 161 167 160 2 3 Sample Output 6 ...
- 【BZOJ2728】[HNOI2012]与非 并查集+数位DP
[BZOJ2728][HNOI2012]与非 Description Input 输入文件第一行是用空格隔开的四个正整数N,K,L和R,接下来的一行是N个非负整数A1,A2……AN,其含义如上所述. ...
- vue中handsontable 使用
handsontable是目前在前端界最接近excel的插件,可以执行编辑,复制粘贴,插入删除行列,排序等复杂操作 1.安装模块包 npm install handsontable-pro @hand ...
- elasticsearch从入门到出门-04-入门的几个需求练手
第一个分析需求:计算每个tag下的商品数量 GET /ecommerce/product/_search{ "aggs": { "group_by_tags&qu ...
- apache虚拟主机配置: 设置二级目录访问跳转
<VirtualHost *:> DocumentRoot "d:/www/abc" ServerName www.abc.com Alias /course &quo ...
- 改善程序与设计的55个具体做法 day3
条款07:为多态基类声明virtual析构函数 任何一本C++语法教材上都会讲这一点(如果没讲,扔掉它),这么做到原因是可以让delete pBase操作能够正确的执行子类的析构函数. 需要说明的是当 ...
- ubuntu14.04 安装pip vitualenv flask
安装pip: $ apt-get install python-pip$ pip -V #查看版本 确认安装成功 安装完pip后,会发现下载的速度特别慢.按如下修改: $ vim ~/.pip/pip ...