iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring3之 bean AutoWi
  • 586
分享到

Spring3之 bean AutoWi

beanAutoWi 2023-01-31 01:01:15 586人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

Autowiring collaborators 自动装配 spring通过检查BeanFactory中的内容,来替指定其他被依赖的bean 优点: 1、显著减少配置的数量 2、以使配置与java代码同步更新 XML配置过程中可在<b

Autowiring collaborators 自动装配
spring通过检查BeanFactory中的内容,来替指定其他被依赖的bean
优点:
1、显著减少配置的数量
2、以使配置与java代码同步更新
XML配置过程中可在<bean>标签中指定autowire属性,它有5个值(3中官方英文文档中只有前4个):
no :No autowiring,bean之间的关系必须通过ref标签来指定
byName :根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。
byType :如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"让Spring抛出异常。
constructor :与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
@deprecated,autowire="autodetect"这个在spring2.5文档中还有见,但是在3中已经没了,3中的XML配置中添加些值会报错。
autodetect :通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。
但是在spring3的xsd文件中找到default这个值项,应该是替代autodetect 的吧:
spring3.0Xsd 文件中
default-autowire的属性只有no ,byName ,byType ,byType 这里既没有autodetect 也没有default
default-autowire-candidates 中值的设置解释:
A default bean name pattern for identifying autowire candidates:
e.g. "*Service", "data*", "*Service*", "data*Service".
Also accepts a comma-separated list of patterns: e.g. "*Service,*Dao".
See the documentation for the 'autowire-candidate' attribute of the
'<bean/>' element for the semantic details of autowire candidate beans.
但是,我测试了下,发现并没有作用?搜到一外文网页上说如同上写法,但是那是07年的帖子了。是不是spring3中这个配置没了作用?
将bean排除在自动装配之外
一、当采用XML格式配置bean时,<bean/>元素的 autowire-candidate属性可被设为false,这样容器在查找自动装配对象时将不考虑该bean。
二、使用对bean名字进行模式匹配来对自动装配进行限制。其做法是在<beans/>元素的'default-autowire-candidates'属性中进行设置。比如,将自动装配限制在名字以'Repository'结尾的bean,那么可以设置为"*Repository“。对于多个匹配模式则可以使用逗号进行分隔。注意,如果在bean定义中的'autowire-candidate'属性显式的设置为'true' 或 'false',那么该容器在自动装配的时候优先采用该属性的设置,而模式匹配将不起作用。
< !--EndFragment-->
测试代码:
com.spring305.test.autowire.po.IdCard.java
Java代码 复制代码 收藏代码
  1. public class IdCard {
  2. private String cardNo;
  3. private String desc;
  4. 。。。getter .setter
  5. }
public class IdCard {
	private String cardNo;
	private String desc;
。。。getter .setter
}
com.spring305.test.autowire.po.People.java
Java代码 复制代码 收藏代码
  1. public class People {
  2. private int id;
  3. private String name;
  4. private IdCard idCard;
  5. public People(){
  6. }
  7. //autowire="constructor" 要有此构造方法
  8. public People(IdCard idCard){
  9. this.idCard = idCard;
  10. }
  11. getter setter
  12. }
public class People {
	private int id;
	private String name;
	private IdCard idCard;
	public People(){
	}
	
	//autowire="constructor" 要有此构造方法
	public People(IdCard idCard){
		this.idCard = idCard;
	}
getter setter
}
src/testAutoWire.xml
Xml代码 复制代码 收藏代码
  1. <beans.... Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
  2. " default-autowire-candidates="*idCard" >
  3. <!-- default-autowire-candidates="*ple*,*ard*" 配置了,但是没有感觉到作用,并没有限制相应的 -->
  4. <!--beans标签中加上: default-autowire="default" or default-autowire="byName" (后面的是延迟加载,就不是了)default-lazy-init="true" -->
  5. <bean id="idCard" class="com.spring305.test.autowire.po.IdCard"></bean>
  6. <bean id="people" class="com.spring305.test.autowire.po.People" autowire="byName"></bean>
  7. <!--
  8. dependency-check="object" 这个在spring3中貌似没了
  9. 单个autowire
  10. <bean id="people" class="com.spring305.test.autowire.po.People" autowire="byName"></bean>
  11. <bean id="people" class="com.spring305.test.autowire.po.People" autowire="constructor"></bean>
  12. autowire-candidate="false" 不会自动注入到其它的bean中
  13. <bean id="idCard" class="com.spring305.test.autowire.po.IdCard" autowire-candidate="false"></bean>
  14. -->
<beans.... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
     "   default-autowire-candidates="*idCard" >
     <!--  default-autowire-candidates="*ple*,*ard*" 配置了,但是没有感觉到作用,并没有限制相应的 -->
     <!--beans标签中加上:  default-autowire="default"  or default-autowire="byName"  (后面的是延迟加载,就不是了)default-lazy-init="true" -->
     
    <bean id="idCard" class="com.spring305.test.autowire.po.IdCard"></bean>
    <bean id="people" class="com.spring305.test.autowire.po.People" autowire="byName"></bean>
    
    <!--
     dependency-check="object"  这个在spring3中貌似没了
     
     单个autowire
    <bean id="people" class="com.spring305.test.autowire.po.People"  autowire="byName"></bean>
     
     <bean id="people" class="com.spring305.test.autowire.po.People"   autowire="constructor"></bean>
     
     autowire-candidate="false" 不会自动注入到其它的bean中
    <bean id="idCard" class="com.spring305.test.autowire.po.IdCard"  autowire-candidate="false"></bean>
     -->
测试:
Java代码 复制代码 收藏代码
  1. //@Test//xml
  2. public void AutoWireTestXML() {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("testAutoWire.xml");
  4. People people = (People) context.getBean("people");
  5. IdCard idCard = people.getIdCard();
  6. // autowire="byName" 当people bean上没有加这个标签时,打印值为null,加后则打印出了对象
  7. System.out.println(idCard);
  8. }
	//@Test//xml
	public void AutoWireTestXML() {
		ApplicationContext context =  new ClassPathXmlApplicationContext("testAutoWire.xml");
		People people = (People) context.getBean("people");
		IdCard idCard = people.getIdCard();
		// autowire="byName" 当people bean上没有加这个标签时,打印值为null,加后则打印出了对象
		System.out.println(idCard);
	}
annotation测试:
com.spring305.test.autowire.po.IdCardAnnotation.java
Java代码 复制代码 收藏代码
  1. @Configuration
  2. public class IdCardAnnotation {
  3. private String cardNo;
  4. private String desc;
  5. 。。。。getter...setter
  6. }
@Configuration
public class IdCardAnnotation {
	private String cardNo;
	private String desc;
。。。。getter...setter
}
com.spring305.test.autowire.po.PeopleAnnotation.java
Java代码 复制代码 收藏代码
  1. @Configuration
  2. public class PeopleAnnotation {
  3. private int id;
  4. private String name;
  5. private IdCardAnnotation idCardAnnotation;
  6. @Autowired(required = false)
  7. //@Qualifier("idCardAnnotation")
  8. public void setIdCardAnnotation(@Qualifier("idCardAnnotation")IdCardAnnotation idCardAnnotation) {
  9. this.idCardAnnotation = idCardAnnotation;
  10. }
  11. getter.setter
  12. }
@Configuration
public class PeopleAnnotation {
	private int id;
	private String name;
	private IdCardAnnotation  idCardAnnotation;
	@Autowired(required = false)
	//@Qualifier("idCardAnnotation")
	public void setIdCardAnnotation(@Qualifier("idCardAnnotation")IdCardAnnotation idCardAnnotation) {
		this.idCardAnnotation = idCardAnnotation;
	}
getter.setter
}
测试:
Java代码
  1. @Test
  2. public void AutoWireTestAnnotation() {
  3. ApplicationContext context = new AnnotationConfigApplicationContext(IdCardAnnotation.class,PeopleAnnotation.class);
  4. PeopleAnnotation people = (PeopleAnnotation) context.getBean("peopleAnnotation");
  5. IdCardAnnotation idCard = people.getIdCardAnnotation();
  6. System.out.println(idCard);
  7. }

--结束END--

本文标题: Spring3之 bean AutoWi

本文链接: https://www.lsjlt.com/news/183427.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring3之 bean AutoWi
    Autowiring collaborators 自动装配 Spring通过检查BeanFactory中的内容,来替指定其他被依赖的bean 优点: 1、显著减少配置的数量 2、以使配置与java代码同步更新 XML配置过程中可在<b...
    99+
    2023-01-31
    bean AutoWi
  • Spring3属性之useDefault
    今天从网上抄了个Spring MVC的demo. 发现Controller方法上采用@RequestMapping("/hello")作为Request与Controller的映射。但对于/hello.html居然也会走这个方法。不解,调查...
    99+
    2023-01-31
    属性 useDefault
  • Spring装配Bean之XML如何安装配置bean
    这篇文章给大家分享的是有关Spring装配Bean之XML如何安装配置bean的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。创建XML配置规范在使用XML配置前,需要创建一个新的配置规范,就像JavaConfig...
    99+
    2023-05-30
    spring xml bean
  • Spring之Bean标签怎么使用
    今天小编给大家分享一下Spring之Bean标签怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Bean标签基本配置用...
    99+
    2023-07-02
  • Spring装配Bean之如何使用Java代码安装配置bean
    这篇文章主要为大家展示了“Spring装配Bean之如何使用Java代码安装配置bean”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Spring装配Bean之如何使用Java代码安装配置bea...
    99+
    2023-05-30
    spring bean java
  • spring之Bean的生命周期详解
    Bean的生命周期:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁Bean的定义Bean 是 spring 装配的组件模型,一切实体类都可以配置成一个 Bean ,进而就可以在任何其他的 Bean 中使用,一个 Be...
    99+
    2023-05-31
    spring bean 生命周期
  • SpringBoot源码之Bean的生命周期
    入口方法为SpringApplication#run() 1.SpringApplication#run() public ConfigurableApplicationCont...
    99+
    2023-05-15
    SpringBoot之Bean的生命周期 bean生命周期 SpringBean生命周期
  • java JSP开发之Spring中Bean的使用
    java JSP开发之Spring中Bean的使用在传统的Java应用中,bean的生命周期很简单。使用Java关键字new进行bean实例化,然后bean就可以被使用了,一旦该bean不再使用,Java就自动进行垃圾回收。然而,在Spri...
    99+
    2023-05-31
    spring bean bea
  • SpringBean生命周期之Bean的注册详解
    目录前言BeanFactory的继承体系Bean的注册alias别名的注册总结前言 上篇文章介绍了Bean元信息的配置与解析过程,限于篇幅Bean注册过程就没展开。 这里主要围绕Be...
    99+
    2024-04-02
  • Spring源码解析之Bean的生命周期
    一、Bean的实例化概述 前一篇分析了BeanDefinition的封装过程,最终将beanName与BeanDefinition以一对一映射关系放到beanDefinitionMa...
    99+
    2024-04-02
  • Java之Bean的生命周期实例分析
    本篇内容主要讲解“Java之Bean的生命周期实例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java之Bean的生命周期实例分析”吧!一、什么是生命周期首先理解下什么是生命周期从创建到消...
    99+
    2023-07-02
  • Spring核心之IOC与bean超详细讲解
    目录前言一、Spring的简介和获取二、依赖注入与IOC1、接口注入2、Setter注入3、构造器注入三、自动装配1、按Bean名称装配2、按bean类型装配四、bean的作用域1、...
    99+
    2022-11-13
    Spring IOC Spring bean
  • Spring中bean的生命周期之getSingleton方法
    Spring中bean的生命周期 要想讲清楚spring中bean的生命周期,真的是不容易,以AnnotationConfigApplicationContext上下文为基础来讲解b...
    99+
    2024-04-02
  • Spring入门到精通之Bean标签详解
    目录Bean标签基本配置Bean标签范围配置Bean生命周期配置Bean的依赖注入入门Bean的依赖注入概念Bean的依赖注入方式Bean的依赖注入的数据类型引入其他配置文件(分模块...
    99+
    2024-04-02
  • 详解Spring系列之@ComponentScan批量注册bean
    目录回顾本文内容@ComponentScan基本原理和使用基本原理使用案例定义组件定义配置类容器扫描和使用@ComponentScan进阶使用源码简析案例1:使用Filters过滤案...
    99+
    2024-04-02
  • Java面试突击之Bean作用域详解
    目录1.作用域2.作用域分类2.1 singleton2.2 prototype2.3 request2.4 session2.5 application3.作用域设置总结Sprin...
    99+
    2024-04-02
  • Spring IOC源码之bean的注册过程讲解
    目录BeanDefition加载注册过程进入obtainFreshBeanFactory方法​进入AbstractRefreshableApplicationContex...
    99+
    2024-04-02
  • SpringBean生命周期之Bean的实例化详解
    目录前言实例化前阶段实例化阶段实例化后阶段总结前言 上一节说到了BeanDefinition的合并过程,这节该说Bean的实例化过程了。根据AbstractAutowireCapab...
    99+
    2024-04-02
  • Java基础之Bean的创建、定位和使用
    目录一、前言二、自动装配Bean2.1 注册Bean2.2 使用Bean三、手动装配3.1 Java类四、XML文件五、导入配置5.1 Java类六、导入到XML一、前言 Bean是...
    99+
    2024-04-02
  • SpringBoot源码之Bean的生命周期是什么
    本文小编为大家详细介绍“SpringBoot源码之Bean的生命周期是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot源码之Bean的生命周期是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知...
    99+
    2023-07-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作