- 浏览: 414777 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (71)
- spring-->>备忘笔记 (17)
- struts-->>备忘笔记 (4)
- hibernate-->>备忘笔记 (6)
- J2EE (10)
- linux (4)
- AOP (3)
- ibatis (1)
- bean (1)
- scop (1)
- IOC (2)
- Central Authentication Service (5)
- javascript-->>备忘笔记 (12)
- 程序人生 (2)
- MongoDB-->>备忘笔记 (2)
- java-->>静态与非静态语句块 (1)
- java-->>泛型 (1)
- java-->>线程 (4)
- java-->>堆栈 (1)
- java-->>jvm (1)
- java-->>工具类 (3)
- mysql-->>备忘笔记 (2)
- 设计模式-->>备忘笔记 (1)
- oracle-->>备忘笔记 (0)
- 互联网->>电子商务 (5)
最新评论
-
lihaiming:
shengouqiang 写道例如有两个线程同时执行(没有sy ...
java之yield(),sleep(),wait()区别详解-备忘笔记 -
zjxchase:
你的onlyMoney这个方法好像不太好用
js 之 Number 工具类 -
shengouqiang:
例如有两个线程同时执行(没有synchronized)一个线程 ...
java之yield(),sleep(),wait()区别详解-备忘笔记 -
u011028234:
楼主你这例子里边的SampleResource实体没有额?
spring 控制反转与依赖注入原理-学习笔记 -
yueerba:
[flash=200,200][url][url][img][ ...
spring 控制反转与依赖注入原理-学习笔记
spring 自动装配注解模式
1、什么是自动装配
2、自动装配的意义
3、自动装配有几种类型
4、如何启用自动装配
5、自动装配将引发的问题
1、什么是自动装配?
3 自动装配有几种类型?
Spring reference 写道
Mode Explanation
no No autowiring at all. Bean references must be defined via a ref element. This is the default,
and changing this is discouraged for larger deployments, since explicitly specifying collaborators gives
greater control and clarity. To some extent, it is a form of documentation about the structure of a system.
byName Autowiring by property name. This option will inspect the container and look for a bean named exactly
the same as the property which needs to be autowired. For example, if you have a bean definition which is
set to autowire by name, and it contains a master property(that is, it has a setMaster(..) method), Spring
will look for a bean definition named master, and use it to set the property.
byType Allows a property to be autowired if there is exactly one bean of the property type in the
container. If there is more than one, a fatal exception is thrown, and this indicates that you
may not use byType autowiring for that bean. If there are no matching beans, nothing
happens; the property is not set. If this is not desirable, setting the dependency-check="objects"
attribute value specifies that an error should be thrown in this case.
constructor This is analogous to byType, but applies to constructor arguments. If there isn't exactly one
bean of the constructor argument type in the container, a fatal error is raised.
autodetect Chooses constructor or byType through introspection of the bean class. If a default
constructor is found, the byType mode will be applied.
引用
模式 说明
no 默认不使用autowiring。 必须显示的使用"<ref />"标签明确地指定bean合作者,对于部署给予更大的
控制和明了。
byName 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自
动装配。例如,在bean定义中将 autowire设置为by name,而该bean包含master属性(同时提供
setMaster(..)方法),Spring就会查找名为master的bean定义,并用它来装配给master属性。
byType 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的
bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,
则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置
dependency-check="objects"让Spring抛出异常。
constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类
型一致的bean,那么将会抛出异常。
autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。
如果发现默认的构造器,那么将使用byType方式。
§4 如何启用自动装配?
你可以参照以下的配置去启用自动装配
引用
<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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default- >
当然,这里的byType你可以更改为其他你想要的装配类型。
§5 自动装配将引发的问题?
Spring reference 写道
Some disadvantages of autowiring:
? Autowiring is more magical than explicit wiring. Although, as noted in the above table, Spring is careful to
avoid guessing in case of ambiguity which might have unexpected results, the relationships between your
Spring-managed objects are no longer documented explicitly.
? Wiring information may not be available to tools that may generate documentation from a Spring container.
引用
自动装配的一些缺点:
尽管自动装配比显式装配更神奇,但是,正如上面所提到的,Spring会尽量避免在装配不明确的时候进行猜测,因为装配不明确可能出现难以预料的结果,而且Spring所管理的对象之间的关联关系也不再能清晰的进行文档化。
对于那些根据Spring配置文件生成文档的工具来说,自动装配将会使这些工具没法生成依赖信息。
自动装配可以减轻配置的工作量,但同时使得配置文件的可读性变得很差,因为你不可能从配置文件
中获知这些对象之间得依赖关系,从而维护困难!
注意:
当根据类型进行自动装配的时候,容器中可能存在多个bean定义跟自动装配的setter方法和构造器参数类型匹配。这样就会存在模棱两可的问题。如果bean定义不唯一,装配时就会抛出异常。
解决方案(任选其一):
1 放弃使用自动装配,使用显示装配。
2 将bean排除在自动装配之外,
引用
两个功能:
1 通过设定bean定义中的'autowire-candidate'属性显式的设置为'true' 或 'false'来设置其是否为被自动装配
对象。
2 使用对bean名字进行模式匹配来对自动装配进行限制,其做法是在<beans/>元素的
'default-autowire-candidates' 属性中进行设置。可以使用通配符,如以'Repository'结尾的bean,
那么可以设置为"*Repository“。
3 通过在bean定义中设置'primary'属性为'true'来将该bean设置为首选自动装配bean。
如何使用Spring autowire请取决于你的项目设计。
Spring reference 写道
The Spring container is able to autowire relationships between collaborating beans. This means that it is
possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents
of the BeanFactory.
引用
Spring IOC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话 ,可以自动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。
简而言之,就是对于bean当中引用的其他bean不需要我们自己去配置它改使用哪个类,Spring的自动装配可以帮组我们完成这些工作。
2 自动装配的意义?
Spring reference 写道
It is important to understand the various advantages and disadvantages of autowiring. Some advantages of
autowiring include:
? Autowiring can significantly reduce the volume of configuration required. However, mechanisms such as the
use of a bean template (discussed elsewhere in this chapter) are also valuable in this regard.
? Autowiring can cause configuration to keep itself up to date as your objects evolve. For example, if you need
to add an additional dependency to a class, that dependency can be satisfied automatically without the need
to modify configuration. Thus there may be a strong case for autowiring during development, without ruling
out the option of switching to explicit wiring when the code base becomes more stable.
引用
理解自动装配的优缺点是很重要的。其中优点包括:
自动装配能显著减少配置的数量。不过,采用bean模板(见这里)也可以达到同样的目的。
自动装配可以使配置与java代码同步更新。例如,如果你需要给一个java类增加一个依赖,那么该依赖将被自动实现而不需要修改配置。因此强烈推荐在开发过程中采用自动装配,而在系统趋于稳定的时候改为显式装配的方式。
发表评论
-
[spring]事务传播级别隔离级别以及高并发下的应用经验
2012-02-12 01:06 10029事务是逻辑处理原子 ... -
spring声明式事务策略 aop拦截-学习笔记
2011-12-16 17:42 2154声明式事务管理: S ... -
spring中的@Transaction配置详解-学习笔记
2011-12-16 17:42 21320spring中的@Transaction配置详解 1、 ... -
spring AspectJ的Execution表达式-备忘笔记
2011-12-16 23:57 14002Aspectj切入点语法定义 在使用spring框 ... -
spring 配置文件实现AOP-学习笔记
2011-12-15 16:15 1542Spring 对AOP的支持I: Aspect默认情况 ... -
spring JDK的Proxy技术实现AOP功能和CGBLB-学习笔记
2011-12-15 16:05 1818动态代理类(以下简称为代理类)是一个实现在创建类时在运行时指定 ... -
spring容器自动扫面管理bean-学习笔记
2011-12-15 16:42 1509通过在classpath自动扫描方式把组件纳入sp ... -
spring的注入依赖之构造器注入- 学习笔记
2011-12-15 16:44 2126Spring的注入依赖(DI)主要有三种注入方式, ... -
spring 控制反转与依赖注入原理-学习笔记
2011-12-14 22:24 11747在Spring中有两个非常 ... -
spring Bean的作用域scope-学习笔记
2011-12-14 22:09 1790Spring容器最初提供了 ... -
添加xml文件自动提示方法,以spring配置文件为例-备忘录
2011-12-14 22:01 1012用eclipse,在导入相应的包后(有人说不导包也有提示,因为 ... -
基于Struts、Hibernate、Spring 的学习框架-笔记备忘
2011-12-13 20:24 1093基于Struts、Hibernate、Spring 的学 ... -
spring声明式事务策略 aop拦截-学习笔记
2011-12-13 20:20 1485声明式事务管理: Spring提供了声明式事务管理 ... -
三种实例化Spring中Bean对象的方式
2011-12-13 20:17 16111、使用类构造器实例化。 <!-- 使用类构造器实 ... -
spring中的@Transaction配置详解
2011-12-13 20:13 7917spring中的@Transaction配 ... -
SSH整合包详解.Struts2.2.3+Spring3.1.0.M2+Hibernate3.6.6-学习笔记
2011-12-13 20:33 1710SSH整合包详解: a) commons commo ...
相关推荐
spring注解 -----最简单的注解与自动装配例子
黑马程序员-SpringCloud-学习笔记-03-Eureka注册中心
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
黑马程序员-SpringCloud-学习笔记-02-微服务拆分及远程调用
黑马程序员-SpringCloud-学习笔记01-认识微服务
自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot
spring注解开发使用教程--Bean加载控制(二)案例代码
文件内附有学习源码及“四万多字的学习笔记”,本学习笔记中涵盖了,Spring中所学的全部知识点,以及还有一些小的知识点。(笔记很详细,笔记很详细。属于本人舍得删系列)。
Spring 自动装配及其注解 博客:https://blog.csdn.net/u010476739/article/details/76735241
spring 事务基于注解模式
spring注解笔记spring注解笔记spring注解笔记spring注解笔记
spring-data-commons-1.7.2.RELEASEspring-data-jpa-1.5.2.RELEASE-java datajpa
spring data jpa的包。spring-data-commons-1.8.0.RELEASE.jar
ean的自动装配,有4种 (1)no:不做任何操作 (2)byName:根据属性 名 自动装配,设值注入 <bean id="xxx" class="xxx" ></bean> (3)byType:根据属性 类型 自动装配,相同类型多个会抛出异常,设值注入 ...
基于spring boot 的spring-boot-starter-hbase自动注解实现,HbaseTemplate的直接使用
spring-demo10-注解-IOC.zip
本项目为Java项目,使用Spring实现自动装配,根据类型方式实现自动装配。适合初学者了解Spring不同装配方式。
spring-cglib-repack-3.2.5.jar,
SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式-个人笔记文档(基础篇)
Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-最新最新