-
spring 自动装配 bean 有哪些方式?
2019-07-17 10:59:39spring 自动装配 bean 有哪些方式?...default - 默认的方式和 "no" 方式一样 no - 不自动装配,需要使用 <ref />节点或参数 byName - 根据名称进行装配 byType - 根据类型进行装配 constructo...spring 自动装配 bean 有哪些方式?
spring 配置文件中 <bean> 节点的 autowire 参数可以控制 bean 自动装配的方式
- default - 默认的方式和 "no" 方式一样
- no - 不自动装配,需要使用 <ref />节点或参数
- byName - 根据名称进行装配
- byType - 根据类型进行装配
- constructor - 根据构造函数进行装配
文档解释
Attribute : autowire Controls whether bean properties are "autowired". This is an automagical process in which bean references don't need to be coded explicitly in the XML bean definition file, but rather the Spring container works out dependencies. The effective default is "no". There are 4 modes: 1. "no" The traditional Spring default. No automagical wiring. Bean references must be defined in the XML file via the <ref/> element (or "ref" attribute). We recommend this in most cases as it makes documentation more explicit. Note that this default mode also allows for annotation-driven autowiring, if activated. "no" refers to externally driven autowiring only, not affecting any autowiring demands that the bean class itself expresses. 2. "byName" Autowiring by property name. If a bean of class Cat exposes a "dog" property, Spring will try to set this to the value of the bean "dog" in the current container. If there is no matching bean by name, nothing special happens. 3. "byType" Autowiring if there is exactly one bean of the property type in the container. If there is more than one, a fatal error is raised, and you cannot use byType autowiring for that bean. If there is none, nothing special happens. 4. "constructor" Analogous to "byType" for constructor arguments. If there is not exactly one bean of the constructor argument type in the bean factory, a fatal error is raised. Note that explicit dependencies, i.e. "property" and "constructor-arg" elements, always override autowiring. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition. It can be shared through the 'default-autowire' attribute at the 'beans' level and potentially inherited from outer 'beans' defaults in case of nested 'beans' sections (e.g. with different profiles). Data Type : string Default Value : default Enumerated Values : - default - no - byName - byType - constructor
代码示例
1、no 方式
spring 配置文件,使用 ref 参数注入 bean,必须要有对象的 setter 方法,这里即 Person 的 setFr 方法。
没有 <property name="fr" ref="fr"></property> 因没有注入 fr 属性,会报空指针错误。
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="no"> <property name="fr" ref="fr"></property> </bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>
鱼竿 bean
package constxiong.interview.assemble; /** * 鱼竿 * @author ConstXiong * @date 2019-07-17 09:53:15 */ public class FishingRod { /** * 被使用 */ public void used() { System.out.println("钓鱼..."); } }
人 bean
package constxiong.interview.assemble; /** * 人 * @author ConstXiong * @date 2019-07-17 09:54:56 */ public class Person { private FishingRod fr; /** * 钓鱼 */ public void fish() { fr.used(); } public void setFr(FishingRod fr) { this.fr = fr; } }
测试代码
package constxiong.interview.assemble; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AssembleTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring_assemble.xml"); Person person = (Person)context.getBean("person"); person.fish(); } }
2、byName 也是需要相应的 setter 方法才能注入
修改 spring 配置文件 autowire="byName"
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="byName"></bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>
其他不变
3、byType 也是需要相应的 setter 方法才能注入
修改 spring 配置文件 autowire="byType"
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="byType"></bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>
其他不变
4、constructor 无需 setter 方法,需要通过 构造方法注入 bean
修改 spring 配置文件autowire="byType"
Person 类去除 setFr 方法,添加构造方法设置 fr 属性
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="constructor"></bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>
package constxiong.interview.assemble; /** * 人 * @author ConstXiong * @date 2019-07-17 09:54:56 */ public class Person { private FishingRod fr; public Person(FishingRod fr) { this.fr = fr; } /** * 钓鱼 */ public void fish() { fr.used(); } }
1、2、3、4 的测试结果一致,打印
钓鱼...
- Java 自学指南
- Java 面试题汇总PC端浏览【点这里】
- Java 面试题汇总小程序浏览,扫二维码
【Java面试题与答案】整理推荐
-
spring的5种自动装配方式
2018-06-10 18:51:22set注入和构造注入有时在做配置时比较麻烦。所以框架为了提高开发效率,...标签的autowire属性自动装配属性的5种选择: 1,no不支持自动装配功能2,default表示默认采用上一级标签的自动装配的取值。如果存在多个配...set注入和构造注入有时在做配置时比较麻烦。所以框架为了提高开发效率,提供自动装配功能,简化配置。Spring框架式默认不支持自动装配的,要想使用自动装配需要修改spring配置文件中<bean>标签的autowire属性
自动装配属性的5种选择:
1,no
不支持自动装配功能
2,default
表示默认采用上一级标签的自动装配的取值。如果存在多个配置文件的话,那么每一个配置文件的自动装配方式都是独立的。
3,byName
从Spring环境中获取目标对象时,目标对象中的属性会根据名称在整个Spring环境中查找<bean>标签的id属性值。如果有相同的,那么获取这个对象,实现关联。
注:整个Spring环境:表示所有的spring配置文件中查找,那么id不能有重复的
4,constructor
使用构造方法完成对象注入,其实也是根据构造方法的参数类型进行对象查找,相当于采用byType的方式。
5,byType
从Spring环境中获取目标对象时,目标对象中的属性会根据类型在整个spring环境中查找<bean>标签的class属性值。如果有相同的,那么获取这个对象,实现关联。
缺点:如果存在多个相同类型的bean对象,会出错。
如果属性为单一类型的数据,那么查找到多个关联对象会发生错误。
如果属性为数组或集合(泛型)类型,那么查找到多个关联对象不会发生异常。
注:自动装配功能和手动装配要是同时使用,那么自动装配就不起作用。在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性。 然而, 若只希望装配个别属性时,autowire 属性就不够灵活了.。
autowire 属性要么根据类型自动装配,,要么根据名称自动装配,不能两者兼而有之。我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。
-
Spring学习笔记(三)使用xml方式及注解方式进行自动装配
2020-07-17 16:24:37Spring中有三种装配方式: 1.xml中显式配置 2.java中显式配置 3.隐式自动装配。自动装配是Spring满足bean依赖的一种方式,Spring会在上下文中自动寻找并自动给bean装配属性 自动装配方式 no(default):不自动装配,...文章目录
概念
Spring中有三种装配方式:
1.xml中显式配置
2.java中显式配置
3.隐式自动装配。自动装配是Spring满足bean依赖的一种方式,Spring会在上下文中自动寻找并自动给bean装配属性
自动装配方式
-
no(default):不自动装配,需使用ref参数注
-
byName:根据名称自动装配
-
byType:根据类型自动装配
-
constructor:根据构造函数自动装配
示例
①使用xml方式装配
1.新建两个实体类Bond和User,Bond被User依赖引用
public class Bond { public void price() { System.out.println("rise"); } }
public class User { private Bond bond; private String name; //自动生成set、get、toString方法 }
2.编写Spring配置文件ApplicationContext.xml,使用参数ref注入bean
<bean id="bond" class="com.xia.pojo.Bond"/> <bean id="user" class="com.xia.pojo.User"> <property name="bond" ref="bond"/> <property name="name" value="张三"/> </bean>
3.测试
public class Test1 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); User user = (User) context.getBean("user"); user.getBond().price(); } }
1.使用byName方式自动装配
修改配置文件ApplicationContext.xml,添加参数autowire=“byName”,省去ref参数注入bean,自动注入对象必须要有set 方法。
使用byName方式后,会自动在容器上下文查找和自己bean对象类中set方法里的值所对应的bean id,若配置文件中注册的bean id相同,则自动配置,保证bean的id唯一且bean和自动注入的属性的set方法中的值一致即可
<bean id="bond" class="com.xia.pojo.Bond"/> <bean id="user" class="com.xia.pojo.User" autowire="byName"> <property name="name" value="张三"/> </bean>
2.使用byType方式自动装配
修改配置文件ApplicationContext.xml,添加参数autowire=“byType”,自动注入对象必须要有set 方法。
使用byName方式后,会自动在容器上下文查找和自己bean对象属性类型相对应的bean,若bean类型相同,则自动配置。不需要保证bean id相对应,bean id甚至可以省略,但必须保证该类型全局唯一,否则报错
<bean id="bond" class="com.xia.pojo.Bond"/> <bean id="user" class="com.xia.pojo.User" autowire="byType"> <property name="name" value="张三"/> </bean>
3.使用constructor自动装配
修改配置文件ApplicationContext.xml,添加参数autowire=“constructor”,需要自动注入的Bean对象类中可以省去set方法。
<bean id="bond" class="com.xia.pojo.Bond"/> <bean id="user" class="com.xia.pojo.User" autowire="constructor"> <property name="name" value="张三"/> </bean>
②使用注解自动装配
注解使用前提
1.在Spring配置文件的命名空间中导入context约束
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
2.开启注解支持【重点!易忘】
<context:annotation-config/>
完整配置文件即
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>
使用注解@Autowired自动装配
修改配置文件,bean中的依赖等属性不需再进行配置
<bean id="bond" class="com.xia.pojo.Bond"/> <bean id="user" class="com.xia.pojo.User"/>
在需要自动注入的对象类属性或者set方法上添加注解@Autowired,也可以省去set方法,前提是该自动装配的属性在IoC容器中存在,注意配置文件中id要和对象类属性名称相同,规则类似于byName,根据名称查询
public class User { @Autowired private Bond bond; private String name; //自动生成set、get、toString方法 }
@Autowired存在一个属性required,若配置**@Autowired(required=false)** ,则说明该对象可以为null;默认required为true,即必须存在对象,不可为空值。使用注解@Nullable同样可以标记该方法/字段/参数允许为null
public class User { @Autowired(required=false) private Bond bond; private String name; //自动生成set、get、toString方法 }
若@Autowired自动装配环境较为复杂,单独一个注解@Autowired不可正常自动装配,即同时注册了多个相同类型bean,不再满足byName规则时,可以配合使用注解@Qualifier来指定一个bean id进行注入
public class User { @Autowired @Qualifier(value = "bond") private Bond bond; private String name; //自动生成set、get、toString方法 }
使用注解@Resource自动装配
java的注解实现Bean注入。与注解@Autowired使用方法类似,在对象属性上添加注解@Resource即可使用,同样配置中不需手动写入值
<bean id="bond" class="com.xia.pojo.Bond"/> <bean id="user" class="com.xia.pojo.User"/>
public class User { @Resource private Bond bond; private String name; //自动生成set、get、toString方法 }
注解@Resource默认先通过byName的方式查找,若找不到则通过byType的方式实现。两个都找不到就会报错。
与注解@Autowired搭配@Qualifier类似,在自动装配环境复杂导致难以满足byName或byType的规则时,可以通过设置@Resource的属性name来指定一个bean id来进行自动装配
public class User { @Resource(name = "bond") private Bond bond; private String name; //自动生成set、get、toString方法 }
@Autowired与@Resource的异同
- 都可通过在对象属性上添加注解进行自动装配
- @Autowired默认通过byType的规则装配,要求该对象必须存在不为空(但可以设置required属性为false使之允许为空),若想使用名称装配可以结合@Qualifier注解进行使用
- @Resource先通过byName的规则查找,找不到名称相对应的就通过byType的规则实现,都找不到就报错(但可以通过设置name属性指定bean id实现)
-
-
Spring的自动装配方式【4.3.16.RELEASE版】
2018-05-03 13:51:05摘自官网:... Autowiring modesModeExplanationno(Default) No autowiring. B...摘自官网:
https://docs.spring.io/spring/docs/4.3.16.RELEASE/spring-framework-reference/htmlsingle/#overview-getting-started-with-spring
Table 7.2. Autowiring modes
Mode Explanation no
(Default) No autowiring. Bean references must be defined via a
ref
element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.默认方式,不启用自动装配。必须通过ref方式引入bean。建议保持默认方式,以确保可控性和明确性。
byName
Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named
master
, and uses it to set the property.通过属性名称自动装配。如果一个bean含有一个master属性,则Spring会寻找命名为master的bean并将其通过setMaster(...)方法设置属性。
byType
Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set.
通过属性类型自动装配。如果发现一个以上的相同类型存在,则报致命性异常。没有找到匹配的beans,不设置属性。
constructor
Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
类似于byType,通过构造函数参数引入。如果找不到正确的bean,则会报致命错误。
-
Java乔晓松-spring自动装配Bean的4种方式
2013-05-07 08:54:50spring自动装配Bean有4种方式: 第一种:autowire="default"即no xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.spri -
Spring自动装配
2020-11-02 09:25:00default - 默认的方式和 “no” 方式一样 no - 不自动装配,需要使用 节点或参数 byName - 根据名称进行装配 byType - 根据类型进行装配 constructor - 根据构造函数进行装配 自动装配的概念 1)手动装配:以value或... -
Java Spring的autowire自动装配bean的四种方式
2013-05-07 08:50:53default <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="... -
2.Spring完成自动装配,两种方式:applicationContext-common.xml
2012-08-29 08:40:20Spring完成自动装配,两种方式 default-autowire="byName" default-autowire="byType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" -
Spring的autowire自动装配bean的四种方式
2013-05-07 22:37:29default [html] view plaincopyprint? ... -
Spring 学习—— Bean装配(Bean 自动装配)
2018-06-18 23:59:10一、Bean 的自动装配 1、No : Default 不做任何操作。 2、byname:根据 Id 名进行自动装配,此选项将检查容器并根据名字查找与属性完全一致的 bean,并将其与属性自动装配 3、byType:如果容器中存在一个与指定类... -
spring 自动装配 学习总结
2018-02-01 16:38:18自动装配分为两种方式 autowired属性与@Autowired注解,注解是对属性的实现,需要先声明注解。所谓自动装配就是区别与以前的手动装配属性与内部bean方式。 autowired属性有6中方式 1、default 我没查过,但是... -
spring的自动装配
2017-05-22 10:51:42自动装配: 无需在spring配置文件中描述javabean之间要建立的依赖关系。...设置方式: 配置或default-autowire=“";这两个配置都可以; 如果在用constructor自动装配的话要在引用的类里写构造方法。 -
spring--bean自动装配
2019-02-19 19:31:48创建应用对象之间协作关系的行为被...的方式来装配了依赖对象. xml中通过autowire=""实现自动装配 1.default(beans这个标签的default-autowired属性) 2.通过byName自动装配 就是通过Bean... -
Spring的自动装配
2011-05-08 09:00:12自动装配的形式有byType、byName、no、construtor、autodetect和default(beans标签中没有,而bean标签中有该属性),共6种装配的方式。 [b]byType[/b]:在容器中寻找一个与需要自动装配的属性类型相同的Bean,... -
11级_Java_曹建波 05.06 Spring的autowire自动装配bean的四种方式
2013-05-07 08:50:00default <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="... -
spring——自动装配
2020-09-12 21:04:442、要实现自动注入(即自动找到相应的对象类型进行匹配注入),在xml头里面加上 default-autowire=“byType” (目前只有这一种方式)即可实现自动注入 自动装配的模式有4种:none,byName,byType,constructor 3、 ... -
Spring容器的自动装配
2016-10-11 11:00:34通常来说,在Spring容器中一个Bean依赖注入另一个Bean,我们需要在该bean中添加等等方式来配置,...Spring容器的自动装配开启方式有两种:beans的default-autowire属性和bean的autowire属性,分别是全局开启和指定be -
Spring——各种类型的自动装配
2020-04-10 11:08:56自动装配 通过约定自动赋值 原则:约定优于配置 只有对象之间的依赖关系可以自动装配(自动装配只... byType:其他bean的类型(class)是否与该course类的ref属性类型一致(注意:此种方式,必须满足:当前ioc容... -
spring的自动装配(NO模式)
2019-09-14 14:02:16spring的自动装配从来都不是在需要注入的地方加上注解,这一点可以从基于xml的方式注入来看出,只需要设置default-autowire属性就够了,默认是no模式 下面是4种模式的官方解释 如果是使用非xml方式来配置... -
Spring Bean的自动装配和资源管理
2017-07-29 16:22:10AutoWiring 在全局属性中声明;default-auotowire="ByName" 属性: ...ByName:根据属性名称自动...根据类型自动装配(属性类型),同上,如果存在多个类型相同的Type,将会抛出异常,并指出不能通过此方式装配,如 -
spring实战-条件装配bean
2017-08-10 21:55:31在做大型项目时,我们的系统会有多个运行环境,如开发人员...强大的Spring为我们提供了条件化装配Bean和profile的bean装配 * profile的需要两个参数:spring.profile.active和Spring.profile.default * 有多种方式配 -
6、Spring核心-ioc容器管理-基于xml的自动装配
2020-05-15 16:56:41首先能看到,自动提示显示了这几种方式,首先default是默认的意思,那默认肯定就是不自动装配嘛,因为不写一样不能自动装配,那就按顺序来测试下这下面三种自动装配模式 1、byName 首先看到这个byName,很多人以后... -
mybatis笔记②——SqlSessionTemplate的自动装配和Mapper的bean的实例化流程
2019-09-14 12:53:40spring的自动装配从来都不是在需要注入的地方加上注解,这一点可以从基于xml的方式注入来看出,只需要设置default-autowire属性就够了,默认是no模式 下面是4种模式的官方解释 如果是使用非xml方式来配置... -
Spring学习七----------Bean的配置之自动装配
2017-04-13 18:16:00参考:Spring学习三----------注入方式 default:优先执行constructor,然后byType,最后byName byName:根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配 byT... -
Spring学习总结(六):基于XML装配Bean(4)
2021-01-18 14:26:21一、自动装配 如果一个bean里有很多...标签有两种方式: 可以配置在<beans>根标签下,表示对全局<bean>起作用,属性名为default-autowire 可以配置在<bean>标签下,表示对当前<bean>... -
Spring之Controller使用篇1模型自动装配对象或集合
2017-09-06 15:21:00@RequestMapping的使用十分重要,这个注解即可以放在方法上,不用显式声明提交方式,比如像这样: @RequestMapping("/spittles") public List<Spittle> spittles(@RequestParam(value = "max", default... -
Spring:bean的生命周期、自动装配、bean之间的继承和依赖
2015-12-08 14:50:30指定bean的作用域 singleton表示单例模式,每次获取的都是同一个实例。可以通过设置lazy-init属性标明是否...prototype表示原型模式,每次获取的都是新的实例,一般设置action为这种方式。 其他的三种针对WEB应用有 -
Spring依赖注入的三种方式
2014-02-14 13:16:00看过几篇关于Spring依赖注入的文章,自己简单总结了一下,大概有三种方式: 1、自动装配 通过配置applicationContext.xml中的标签的default-autowire属性,或者标签的autowire属性,可以配置IOC容器的自动装配机制...
-
MySQL 高可用工具 heartbeat 实战部署详解
-
朱老师C++课程第3部分-3.6智能指针与STL查漏补缺
-
libFuzzer视频教程
-
php获取文件后缀
-
FTP 文件传输服务
-
部门调查:跟踪研究软件的影响-源码
-
cesium地图设备轨迹回放视频.mp4
-
探索新移动服务的演进
-
ISBFrontEnd-源码
-
软件架构入门
-
微博系统中的混合追随者推荐
-
win10安装redis服务小问题说明
-
Awesome-Caffe:令人敬畏的Caffe-源码
-
程序员必修基础套餐课
-
go golang sqlx 获取数据库信息时, sql.NullString 类型在返回时只显示对应数据,不显示其它 Valid
-
3D打印机使用.rar
-
我的机器学习笔记(一)-监督学习vs无监督学习
-
2021-02-25
-
多线程
-
MMM 集群部署实现 MySQL 高可用和读写分离