`
张玉龙
  • 浏览: 721925 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

spring 自动装配 default-autowire="byName/byType"[转]

 
阅读更多
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,推荐用这种,因以上自动装配,对维护不是太好。

分享到:
评论

相关推荐

    spring-autowire-demo.zip

    以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比

    维生药业小项目 SSH简单学习项目

    default-lazy-init="false" default-autowire="byName"&gt; &lt;context:annotation-config/&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt; &lt;property name="driverClass" value=...

    spring-autowire.zip

    在idea2020 下写的spring5-autowired 小例子。person,cat dog ,给大家一个参考。

    封装通用的Spring3+Struts2+MyBatis3的CRUD+条件分页查询,Spring+Quartz调度,FunctionCharts图像化工具

    &lt;bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="getPolicyTime"/&gt; &lt;/list&gt; &lt;/...

    基于SpringJDBC的BaseDAO

    实现了简单的ORM增删改查。... abstract="false" lazy-init="default" autowire="default" dependency-check="default"&gt; &lt;property name="dataSource"&gt; &lt;ref local="dataSource" /&gt; &lt;/property&gt; &lt;/bean&gt;

    Spring3.2.4+Quartz2.2.0 Demo

    &lt;bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="myJobTrigger" /&gt; &lt;/...

    Spring2.0+quartz1.8定时执行任务内含Cron表达式生成器

    &lt;bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt; &lt;property name="triggers"&gt; &lt;ref bean="runTime" /&gt; &lt;/list&gt; &lt;/property&gt;...

    springboot-autowire:学习springboot自动装配原理

    #学习springboot自动装配 ##一,手动装配 ### 1,模式注解装配 @Component注解,或者@Component注解的扩展,@ Controller,@ Service,存储库,@ Configruation等, ### 2. @ Configuration启动容器+ @ Bean注册...

    storm-spring-autowire:使storm支持spring的注入功能

    storm-spring-autowire 使storm支持spring的注入功能 由于storm的生命周期如下 1.在提交了一个topology之后(是在nimbus所在的机器么?), 创建spout/bolt实例(spout/bolt在storm中统称为component)并进行序列化. 2.将...

    Spring自动装配的方式

    Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。

    spring-cloud-kubernetes:Kubernetes与Spring Cloud的集成

    该项目已移至 。 Spring Cloud Kubernetes 与集成 特征 (无论代码在Kubernetes内还是外部运行,它都是透明的) Kubernetes的DiscoveryClient 该项目提供了的的。 这使您可以按名称查询... @Autowire private Discov

    xmljava系统源码-SpringInAction4:《SpringInAction4th》学习笔记

    可以在一个应用上下文中定义多个配置文件,每个配置文件设置自己的默认自动装配策略(default-autowire) 如果使用constructor自动装配策略,就不能混合使用constructor-arg 注解方式可以实现更细粒度的自动装配,...

    grpc-jwt-spring-boot-starter:具有JWT授权的gRPC框架的Spring Boot Starter

    具有Spring Boot Starter-gRPC Java JWT 使用Auth模块扩展出色的。 使用类似于Spring Security模块中使用的简单注释的简单实现。 快速开始 (尝试示例项目:Kotlin中的。) 简单的开始仅由3个简单的步骤组成。 ...

    spring示例代码好又全.rar

    内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...

    autowire::electric_plug:Go依赖注入框架

    :electric_plug: 汽车线 用于Golang的基于反射的依赖项注入工具箱。 该自述文件处于进行中状态。 安装 整个项目基于go模块。 要获取最新版本,请使用go1.16 +并使用...go get github.com/go-autowire/autowire@v1.0.4

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...

    Spring的三种注入方式

    2.autowire=“byName”  3.autowire=bytype    详细解析注入方式  例如:有如下两个类需要注入  第一个类: 1.package org.jia;  2.  3. public class Order {  4. private String orderNum;...

    Jimmy-Ma#SpringDemo#004.自动装配(XML)1

    使用autowire属性指定自动装配的方式byName根据bean的名字和当前bean的setter风格属性名进行自动装配若有匹配,则自动转配若无匹配,则不装配

    Spring的学习笔记

    八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...

    spring2.5学习PPT 传智博客

    13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP...

Global site tag (gtag.js) - Google Analytics