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,推荐用这种,因以上自动装配,对维护不是太好。
分享到:
相关推荐
以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比
default-lazy-init="false" default-autowire="byName"> <context:annotation-config/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value=...
在idea2020 下写的spring5-autowired 小例子。person,cat dog ,给大家一个参考。
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="getPolicyTime"/> </list> </...
实现了简单的ORM增删改查。... abstract="false" lazy-init="default" autowire="default" dependency-check="default"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean>
<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="myJobTrigger" /> </...
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="runTime" /> </list> </property>...
#学习springboot自动装配 ##一,手动装配 ### 1,模式注解装配 @Component注解,或者@Component注解的扩展,@ Controller,@ Service,存储库,@ Configruation等, ### 2. @ Configuration启动容器+ @ Bean注册...
storm-spring-autowire 使storm支持spring的注入功能 由于storm的生命周期如下 1.在提交了一个topology之后(是在nimbus所在的机器么?), 创建spout/bolt实例(spout/bolt在storm中统称为component)并进行序列化. 2.将...
Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。
该项目已移至 。 Spring Cloud Kubernetes 与集成 特征 (无论代码在Kubernetes内还是外部运行,它都是透明的) Kubernetes的DiscoveryClient 该项目提供了的的。 这使您可以按名称查询... @Autowire private Discov
可以在一个应用上下文中定义多个配置文件,每个配置文件设置自己的默认自动装配策略(default-autowire) 如果使用constructor自动装配策略,就不能混合使用constructor-arg 注解方式可以实现更细粒度的自动装配,...
具有Spring Boot Starter-gRPC Java JWT 使用Auth模块扩展出色的。 使用类似于Spring Security模块中使用的简单注释的简单实现。 快速开始 (尝试示例项目:Kotlin中的。) 简单的开始仅由3个简单的步骤组成。 ...
内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...
:electric_plug: 汽车线 用于Golang的基于反射的依赖项注入工具箱。 该自述文件处于进行中状态。 安装 整个项目基于go模块。 要获取最新版本,请使用go1.16 +并使用...go get github.com/go-autowire/autowire@v1.0.4
网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...
2.autowire=“byName” 3.autowire=bytype 详细解析注入方式 例如:有如下两个类需要注入 第一个类: 1.package org.jia; 2. 3. public class Order { 4. private String orderNum;...
使用autowire属性指定自动装配的方式byName根据bean的名字和当前bean的setter风格属性名进行自动装配若有匹配,则自动转配若无匹配,则不装配
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP...