`

spring中autowire的用法

阅读更多

原文链接:http://blog.sina.com.cn/s/blog_44167fca0100e9iu.html

Autowire模式就是在spring的声明文件里用作进行对象间的关联关系自动绑定的,就是在spring beanfactory内的一个bean对其bean的引用可以自动进行,而不一定用ref=的方式显式声明。在reference的3.3.6节有详细的介绍,autowire主要有5种模式:

 

1 no

不使用Autowire,引用关系显示声明,spring的reference也建议不用autoware,因为这会破坏模块关系的可读性,原文如下:

Note: as has already been mentioned, for larger applications, it is discouraged to use autowiring because it

removes the transparency and the structure from your collaborating classes.

 

2 byName

用名称关联,如果指定了这种模式,如

   <bean id="userManagerTarget" class="com.mdcchina.jianghu.logic.UserManager" >

       <property name="baseDAO"/>

   </bean>

这样对于bean userManagerTarget的属性baseDAO,spring就会自动去引用同名的bean,也就是上面的声明和下面是等价的:

   <bean id="userManagerTarget" class="com.mdcchina.jianghu.logic.UserManager" >

       <property name="baseDAO">

          <ref local="baseDAO"/>

       </property>

   </bean>

 

3 byType

和前面的byName类似的,就是这个属性会在整个beanFactory定义里找和这个属性一样的bean自动关联上,如果有2个或更多这个类型的bean在beanFactory的定义里,就直接抛异常了,如果没有,就什么都不发生,这个属性就是null,所以这个只适用与这个属性的类型有且只有一个同类的bean在spring里定义

 

4 constructor

这个的意思我没有确定的把握,不过感觉用途也不会大,好像是用构造函数新建一个属性类型的bean并关联上,reference原文是:

This is analogous to byType, but applies to constructor arguments. If there isn't exactly one

bean of the constructor argument type in the bean factory, a fatal error is raised.

 

5 autodetect

这个的意思好像是说自动决定用byType还是constructor方式,原文如下:

Chooses constructor or byType through introspection of the bean class. If a default

constructor is found, byType gets applied.

 

综上所述,感觉上只有byName比较实用一些,但是spring的reference还是不推荐在定义中用这个功能

分享到:
评论

相关推荐

    Spring实战之@Autowire注解用法详解

    主要介绍了Spring实战之@Autowire注解用法,结合实例形式详细分析了Spring @Autowire注解具体实现步骤与相关使用技巧,需要的朋友可以参考下

    storm-spring-autowire:使storm支持spring的注入功能

    这个项目采取了了在prepare方法中执行注入的方式使bolt在序列化完成后执行注入,绕开了storm的序列化机制 使用这个项目里 你可以通过以下代码做到最基本的注入 @Resource private SentenceCounter counter; public ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring 2.0 开发参考手册

    6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 7.2.3. AspectJ切入点表达式 7.2.4. ...

    Spring的学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring学习笔记(有代码有注解解释)

    Spring 的工厂方法;IoC 自动装载(Autowire);AOP以及如何使用; 适用人群:比较适合与我一样的在校普通大学生进行学习整理,以及适合初学spring的朋友进行巩固加深印象! 阅读建议:需要有一定的代码基础,...

    spring2.5 学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    spring2.5.6源码

    rar包内含有spring2.5.6源码,解压即可使用 源代码分析,是一件既痛苦又快乐的事情,看别人写的代码是通过的,但当你能够看明白的时候,相信快乐也会随之而来,为了减少痛苦,更快的带来快乐,在这里希望通过这篇...

    grpc-jwt-spring-boot-starter:具有JWT授权的gRPC框架的Spring Boot Starter

    具有Spring Boot Starter-gRPC Java JWT 使用Auth模块扩展出色的。 使用类似于Spring Security模块中使用的简单...2.在服务方法中添加@Allow批注 您需要做的就是在服务实现中注释您的方法。 @GRpcService public clas

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    Whether Spring should autoWire or not 是否自动绑定Spring struts.objectFactory.spring.useClassCache Whether Spring should use its class cache or not 是否spring应该使用自身的cache struts....

    springboot-autowire:学习springboot自动装配原理

    被注解的类内部包含一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并同时使用于建造bean定义,初始化Spring容器。...

    java8stream源码-ls-annotations:通过反编译字节码显示所有带有java注解的声明

    标记的函数将获得返回的值由带有@Bean注解的函数调用,前提是该函数返回与@Autowire注解的参数类型相同的类型或派生类,并且带有Bean注解的类包含在@ComponentScan注解中。 相同的机制适用于标有 @Component 注释的 ...

    REST-API-CRM系统:Spring REST-客户关系管理系统的CRUD数据库

    使用@ExceptionHandler添加异常处理程序方法 添加客户 更新客户 删除客户 API积分 @RequestMapping(“ / api”) @GetMapping(“ / customers”) @GetMapping(“ / customers / {customerId}”) @...

    Struts2属性文件详解

    struts.objectFactory.spring.autoWire 指定Spring框架的自动装配模式, 该属性的默认值是name, 即默认根据Bean的name属性自动装配. struts.objectFactory.spring.useClassCache 该属性指定整合Spring框架时,是否...

    解决SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题

    主要介绍了SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题的解决方法,需要的朋友可以参考下

    codekrypt-di:自定义依赖注入框架(类似于Spring)

    Codekrypt依赖注入器特征支持使用@Autowire和@Qualifier现场注入支持@PostConstruct用法HornAirImp.java @Componentpublic class HornAirImpl implements Horn { @Override public void honk () { System ....

    bonecp连接池

    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" autowire="autodetect"&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; ...

Global site tag (gtag.js) - Google Analytics