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 ...
随机推荐
- VS使用WEB DEPLOY发布
背景是这样的,公司有两台服务器,平时一台备用,另一台做为主生产机器.当有大量补丁或者安装什么东西需要重启的时候,交其中一台直接关掉IIS,然后重启即可,此时另一台负责处理用户请求. 之前一台服务器一个 ...
- #region的作用和注释快捷键
让函数在编辑器中收起来,简洁 #region All MenuItems [@MenuItem("xxx")] public static void Init() { XXXXX; ...
- Netty实战
一.Netty异步和事件驱动1.Java网络编程回顾socket.accept 阻塞socket.setsockopt /非阻塞2.NIO异步非阻塞a).nio 非阻塞的关键时使用选择器(java.n ...
- C#数组 多个集合和数组的操作(合并,去重,拆分,判断)
http://www.cnblogs.com/liguanghui/archive/2011/11/09/2242309.html http://www.cnblogs.com/dreamszx/ar ...
- [Catalan数]1086 栈、3112 二叉树计数、3134 Circle
1086 栈 2003年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 栈是计算机中 ...
- 如何基于EasyDSS体系的全套SDK完成各种场景下的视频应用需求
需求背景 回顾EasyDSS的发展过程,基本上保持的是先局部后系统.先组件后平台的发展方式,一步一步夯实每一个细节功能点,从最基础.最兼容的音视频数据的拉流获取,到高效的.全兼容的数据推流,再到流媒体 ...
- android菜鸟学习笔记21----ContentProvider(一)ContentProvider的简单使用
ContentProvider是Android四大组件之一,它用来封装数据,并通过ContentResolver接口将数据提供给其他应用.只有当需要在多个应用之间共享数据时才会用到ContentPro ...
- SMARTFORMS 字段格式化设置
[转自http://lz357502668.blog.163.com/blog/static/16496743201273153434564/] 在SMARTFORM 输出的时候有时候会遇到数字类型无 ...
- JITWatch工具
JITWatch,执行程序时监测Java HotSpot JIT编译器如何运作的工具,有助于做JVM的性能优化 wiki: https://github.com/AdoptOpenJDK/jitwat ...
- python实例3-天气小模块
调用中国天气的一小段代码,抓取 #! /usr/bin/python # coding = utf-8 # ToDo: get weather info from weather.com.cn # A ...