首页 > 其他分享 >spring 自动装配 default-autowire="byName/byType"[转]

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

时间:2023-04-23 17:38:41浏览次数:38  
标签:装配 byType autowire default spring byName


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

标签:装配,byType,autowire,default,spring,byName
From: https://blog.51cto.com/u_16085348/6218417

相关文章

  • Vue Typescript 引入文件接口,就无法使用withDefaults
    就是代码写的不规范报错写法 import{Setting}from'@element-plus/icons-vue' import{defineProps,withDefaults}from'vue' import{PiProject}from'@/types/Project' interfaceProjectCardProps{ project:PiProject } constprops=de......
  • ESM export default {...object} All In One
    ESMexportdefault{...object}AllInOneobjectdestructuring&moduleexportdefaulterrosexport{...obj}❌constMetrics={autoReport,manualReport,};export{...Metrics,};exportdefaultMetrics;demosexportdefault{...obj}......
  • Invalid prop: type check failed for prop "defaultExpandAll". Expected Boolean, g
    vue中使用element-ui报错如下,defaultExpandAll关键词页面也搜不到[Vuewarn]:Invalidprop:typecheckfailedforprop"defaultExpandAll".ExpectedBoolean,gotStringwithvalue"true".foundin---><ElTable>atpackages/table/src/table.vue......
  • context:component-scan扫描使用上的容易忽略的use-default-filters
    评:问题如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean。正确Java代码收藏代码<context:component-scanbase-package="org.bdp.system.test.controller"><context:include-filtertype="annotation"expression="org.spri......
  • Konga versions equal to or below v0.14.9 use the default TOKEN_SECRET
    VulnerabilityDescriptionKongisaclould-native,fast,scalable,distributedmicroserviceabstractionlayer(alsoknownasAPIgateway,APImiddlewareorinsomecasesservicegrid)framework,repositoryaddress:https://github.com/Kong/kong,officialw......
  • Go: default print format
      slice,map即使为nil打印格式和空元素的情况一样,chan,func,interface,pointer为nil时,打印<nil>当chan,func,pointer赋值后,都打印地址,interface赋值后,永远打印其dynamicvalue的格式packagemainimport"fmt"typeIinterface{m()}typeT[]bytefu......
  • Idea解决Could not autowire. No beans of ‘xxxx‘ type found的错误提示
    1.问题描述在Idea的spring工程里,经常会遇到Couldnotautowire.Nobeansof'xxxx'typefound的错误提示。(但程序的编译和运行都是没有问题的,有时候也有可能会报错,无法运行程序),这个错误提示并不会产生影响。但红色的错误提示看起来很不舒服。2.原因原因可能有两个,第一个是......
  • C#Unicode编码不可逆,Default也不可逆
    不可逆:4个eunsafestaticvoidMain(string[]args){byte[]by=newbyte[256];for(inti=0;i<by.Length;i++)by[i]=(byte)i;strings=Encoding.Unicode.GetString(by);byte[]by2=Encoding.Unicode.GetBytes(s);for(inti=0;i<by.Leng......
  • Could not autowire. No beans of BookDao' type found
    在做Spring或者SpringBoot项目时,在测试类中创建一个实体类属性并进行自动装配时,回报红:Couldnotautowire.NobeansofBookDao'typefound(只是环境的原因,不是错误) 直接Alt+Enter,将错误改成警告即可 ......
  • Default Arguments总结
    默认实参默认实参在C++编程实践中非常常见,但其中也有一些有趣的知识点与扩展,本文对默认实参做简单总结;函数默认实参默认实参用来取代函数调用中缺失的尾部实参:voidProcess(intx=3,inty=4){}拥有默认实参的形参之后的形参必须在同一作用域内有声明提供了默认参数,否则编译......