官方网站营销网站开发获取用户微信号登录
news/
2025/9/22 19:18:40/
文章来源:
官方网站营销,网站开发获取用户微信号登录,制作一个软件,郑州地方网络推广网站自动装配(autowiring)有助于减少甚至消除配置property元素和constructor-arg元素#xff0c;让Spring自动识别如何装配Bean的依赖关系。 自动检测(autodiscovery)比自动装配更进了一步#xff0c;让Spring能够自动识别哪些类需要被配置成Spring Bean#xf… 自动装配(autowiring)有助于减少甚至消除配置property元素和constructor-arg元素让Spring自动识别如何装配Bean的依赖关系。 自动检测(autodiscovery)比自动装配更进了一步让Spring能够自动识别哪些类需要被配置成Spring Bean从而减少对bean元素的使用。 1.自动装配属性 1.1 4种类型的自动装配 ● byName——把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中。如果没有跟属性的名字相匹配的Bean则该属性不进行装配。 ● byType——把与Bean的属性具有相同类型的其他Bean自动装配到Bean的对应属性中。如果没有跟属性的类型相匹配的Bean则该属性不被装配。 ● constructor——把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。 ● autodetect——首先尝试使用constructor进行自动装配。如果失败在尝试使用byType进行自动装配。 byName自动装配: bean id kenny class com.ouc.test.springs.Instruments autowire byName property name song value bells /
/bean 为属性自动装配ID与该属性的名字相同的Bean。 byType自动装配: 如果Spring寻找到多个Bean它们的类型与自动装配的属性类型都相匹配Spring不会猜测哪一个Bean更适合自动装配而是会抛出异常。 可以为自动装配标识一个首选Bean或者可以取消某个Bean自动装配的候选资格。为了使用primary属性不得不将非首选Bean的primary属性设置为false。 bean id saxophone class com.ouc.test.springs.Saxophone primary false / primary属性仅对标识首选Bean有意义。 如果希望排除某些Bean可以设置这些Bean的autowire-candidate属性为false。 bean id saxophone class com.ouc.test.springs.Saxophone autowire-candidate false / constructor自动装配: 如果要通过构造器注入来配置Bean可以移除constructor-arg元素由Spring在应用上下文中自动选择Bean注入到构造器入参中。 bean id saxophone classcom.ouc.springs.test.Saxophone autowire constructor / 最佳自动装配: bean id saxophone classcom.ouc.springs.test.Saxophone autowire autodetect / 1.2 默认自动装配 ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsddefault-autowirebyType /beans 2 .使用注解装配 Spring容器默认禁用注解装配。 启用注解装配最简单的方式是使用Spring的context命名空间配置中的context:annotation-config元素。 Spring 3 支持几种不同的用于自动装配的注解 ● Spring自带的Autowired注解 ● JSR-330的Inject注解 ● JSR-250的Resource注解。 2.1 使用Autowired Autowiredpublic void setInstrument(Instrument instrument){this.instrument instrument;} Spring会尝试对该方法执行byType自动装配可以使用Autowired标注需要自动装配Bean引用的任意方法。 可以使用Autowired注解直接标注属性并删除setter方法 Autowiredprivate Instrument instrument; 1)可选的自动装配: 默认情况下Autowired所标注的属性或参数必须是可以装配的。如果没有Bean可以装配到Autowired所标注的属性或参数中自动装配就会失败(抛出NoSuchBeanDefinitionException). 可以通过设置Autowired的required属性为false来配置自动装配是可选的。 Autowired(requiredfalse)private Instrument instrument; 2)限定歧义性的依赖 Qualifier注解缩小了自动装配挑选候选Bean的范围通过指定Bean的ID把选择范围缩小到只剩下一个Bean。 AutowiredQualifier(guitar)private Instrument instrument; 3)创建自定义的限定器(Qualifier) import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.beans.factory.annotation.Qualifier;Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})Retention(RetentionPolicy.RUNTIME)Qualifierpublic interface StringedInstrument{} 2.2 借助Inject实现基于标准的自动装配 Injectprivate Instrument instrument; Inject没有required属性。 限定Inject所标注的属性。 InjectNamed(guitar)private Instrument instrument; Named通过Bean的ID来标识可选择的BeanNamed实际上是一个使用Qualifier注解所标注的注解。 创建自定义的JSR-330 Qualifier import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import javax.inject.Qualifier;Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})Retention(RetentionPolicy.RUNTIME)Qualifierpublic interface StringedInstrument{} 2.3 在注解注入中使用表达式 Value(#{systemProperties.myFavoriteSong})private String song; 3.自动检测Bean 使用context:component-scan元素配置自动检测。 ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/beans/spring-context-3.1.xsd context:component-scan base-packagecom.ouc.springs.test /context:component-scan/beans 3.1为自动检测标注Bean 默认情况下context:component-scan查找使用构造型(stereotype)注解所标注的类这些特殊的注解如下 Component——通用的构造型注解标识该类为Spring组件。 Controller——标识将该类定义为Spring MVC Controller。 Repository——标识将该类定义为数据仓库。 Service——标识将该类定义为服务。 使用Component标注的任意自定义注解。 3.2 过滤组件扫描 通过为context:component-scan配置context:include-filter和context:exclude-filter子元素可以随意调整扫描行为。 context:component-scan base-packagecom.ouc.springs.test context:include-filter typeassignable expressioncom.ouc.springs.tst.Instrument /context:exclude-filter typeannotation expressioncom.ouc.springs.tst.SkipIt /
/context:component-scan 4.使用Spring基于Java的配置 4.1 定义一个配置类 import org.springframework.context.annotation.Configuration;Configurationpublic class SpringIdolConfig{} Configuration注解会作为一个标识告知Spring:这个类将包含一个或多个Spring Bean的定义。 4.2 声明一个简单的Bean Beanpublic Performer duke(){return new Juggler();} Bean告知Spring这个方法将返回一个对象该对象应该被注册为Spring应用上下文中的一个Bean方法名将作为该Bean的ID。转载于:https://www.cnblogs.com/wp5719/p/5136177.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910102.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!