<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]Word Break @ Python

    原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...

  2. vRealize Automation的REST API Reference在哪里可以看到?

    两个地方: 1. VMware官网可以查看. http://pubs.vmware.com/vrealize-automation-71/topic/com.vmware.vra.restapi.do ...

  3. laravel 开启sql调试

    打开app\Providers\AppServiceProvider.PHP,在boot方法中添加如下内容 public function boot() { //sql调试 $sql_debug = ...

  4. xgboost入门与实战

    xgboost入门与实战(实战调参篇) https://blog.csdn.net/sb19931201/article/details/52577592 前言 前面几篇博文都在学习原理知识,是时候上 ...

  5. 在使用Vs2013打开Vs2008的解决方案时出现了以下错误:此版本的应用程序不支持其项目类型(.csproj)

    在使用Vs2013打开Vs2008的解决方案时出现了以下错误: 无法打开 因为此版本的应用程序不支持其项目类型(.csproj). 在网络上找到解决方案: 命令行或者Vs自带的命令提示符输入:deve ...

  6. webstorm和intellij idea下如何自动编译sass和scss文件

    webstorm和intellij idea下如何自动编译sass和scss文件 https://segmentfault.com/a/1190000008996504 https://www.jia ...

  7. GL_ACTIVE_UNIFORMS可能不会返回没有用到的uniform

    To query for the list of active uniforms in a program, you first call glGetProgramiv with the GL_ACT ...

  8. How to Build a Search Page with Elasticsearch and .NET

    Although SQL Server's Full-Text search is good for searching text that is within a database, there a ...

  9. PHP开发框架比较

    PHP开发框架比较 Laravel 是一个简单优雅的 PHP WEB 开发框架,将你从意大利面条式的代码中解放出来.通过简单.优雅.表达式语法开发出很棒的 WEB应用!但是通过使用我们发现Larave ...

  10. MySQL 存储过程/游标/事务

    将会用到的几个表 mysql> DESC products; +------------+--------------+------+-----+---------+-------------- ...