<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"  
  
  
一、spring 自动装配 default-autowire="byName"  
  
byName,按变量名称,与id名称一样,若不一样,就报错。  
  
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"  
           default-autowire="byName">  
  
<bean id="bean1" class="com.zd.bean.Bean1" >  
<!-- 配了default-autowire="byName" ,可以注释  
    <property name="bean2">  
         <ref bean="bean2" />  
    </property>  
    <property name="bean3" ref="bean3" />  
    <property name="bean4">  
        <bean class="com.zd.bean.Bean4">  
            <property name="age" value="16" />  
        </bean>  
    </property>  
-->  
</bean>  
  
二、spring 自动装配 default-autowire="byType"  
  
byType,按类型自动装配,若变量与id不匹配,也没关系  
  
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"  
           default-autowire="byName">  
  
<bean id="bean1" class="com.zd.bean.Bean1" >  
<!-- 配了default-autowire="byType" ,可以注释  
    <property name="bean2">  
         <ref bean="bean2" />  
    </property>  
    <property name="bean3" ref="bean3" />  
    <property name="bean4">  
        <bean class="com.zd.bean.Bean4">  
            <property name="age" value="16" />  
        </bean>  
    </property>  
-->  
</bean>  
  
三、默认配置是no,推荐用这种,因以上自动装配,对维护不是太好。  </PRE><BR>
<BR>
<PRE></PRE>

spring 自动装配 default-autowire=&quot;byName/byType&quot;的更多相关文章

  1. Spring笔记04(DI(给属性赋值),自动装配(autowire))

    给不同数据类型注入值: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  2. Spring 自动装配及其注解

    一.属性自动装配 首先,准备三个类,分别是User,Cat,Dog.其中User属性拥有Cat和Dog对象. package com.hdu.autowire; public class User { ...

  3. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  4. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  5. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  6. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean

    除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...

  7. Spring自动装配(二)

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...

  8. Spring 自动装配

    1.自动装配有 bytype 和byName两种模式. 2.可以使用autowire属性指定自动装配的方式,byName根据bean的名称和当前bean的setter风格属性进行自动装配:byType ...

  9. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

随机推荐

  1. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  2. iOS开发-UITableView自定义Cell

    UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...

  3. SQL操作查漏补缺

    SQL教程地址:http://www.w3school.com.cn/sql/index.asp TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是 ...

  4. walmart weekly sales

    最近参加了kaggle的walmart weekly sales 预测比赛,已经过期但还能提交获得评分.Walmart Recruiting - Store Sales Forecasting 提供的 ...

  5. 经典算法题每日演练——第十一题 Bitmap算法 (转)

    http://www.cnblogs.com/huangxincheng/archive/2012/12/06/2804756.html 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash ...

  6. Redis源代码剖析--对象object

    前面一系列的博客分析了Redis的基本数据结构,有动态字符串sds.双端链表sdlist.字典dict.跳跃表skiplist.整数集合intset和压缩列表ziplist等,这些数据结构对于用户来说 ...

  7. Discuz常见小问题-如何取消登陆发帖验证码

    1 正常情况下,用户点击登录之后,需要填写验证码 2 进入后台,点击防灌水,验证设置,然后下面的各个选项可以设置是否启用验证码.

  8. url文件的格式

    [DEFAULT]BASEURL= [InternetShortcut]URL=WorkingDirectory=ShowCommand=IconIndex=IconFile=Modified=Hot ...

  9. All you should know about NUMA in VMware!

    http://www.elasticvision.info/ All you should know about NUMA in VMware! Lets try answering some typ ...

  10. 深度学习-Windows平台下的Caffe编译教程

    一.安装CUDA7.5 Cuda是英伟达推出的GPU加速运算平台 我这里安装的是cuda7.5,已经安装过的忽略,还没有安装过的这里有安装教程.windows下面安装还是非常简单的. https:// ...