spring di2
来源:互联网 发布:js冲锋枪 编辑:程序博客网 时间:2024/06/03 09:41
<?xml version="1.0" encoding="UTF-8"?>
<!-- 使用Spring的命名空间p装配属性,配置p -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byType">
<!-- 如上两行,为容器中所有的Bean设置初始化和销毁方法 -->
<!--
通过<context:annotation-config />告诉spring我们通过注解进行自动装配。
spring支持集中不同的用于自动装配的注解:
spring自带的@Autowired注解
JSR-330的@Inject注解
JSR-250的@Resource注解
@Autowired
对set方法使用该注解,spring尝试使用byType进行装配
该注解可以标注任意方法,包括构造器
当使用该注解直接标注属性时,即可删除setter方法,同时该注解不会受限于private关键字。
@Qualifier("guitar")指定名为guitar的注入
-->
<context:annotation-config />
<!--
<context:component-scan>除了具有 <context:annotation-config />一样的工作,还允许spring自动
检测Bean和定义Bean
base-package:扫描包
扫描情况下,如何知道是否应该注册为spring Bean呢?
@Component:通用的构造型注解,表示该类是spring组件,如果@Component("aa")标识,则代表bean的id为aa,否则默认类名首字母小写
@Controller:标识将该类定义为Spring MVC Controller
@Repository:标识将该类定义为数据仓库
@Service:标识将该类定义为服务
-->
<context:component-scan base-package="com.bean">
</context:component-scan>
<!--
在扫描获得bean时,通过为<context:component-scan>配置<context:include-filter>和/或者<context:exclude-filter>
-->
<context:component-scan base-package="com.bean">
<context:include-fileter type="assignable" expression="com.bean.Instrument"/>
</context:component-scan>
<!-- Bean declarations go here -->
<!--
自动装配(autowiring):有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让spring自动识别如何装配Bean的依赖关系。
自动检测(autodiscovery):比自动装配更进一步,让spring能够自动识别哪些类需要被装配成spring Bean,从而减少对<bean>元素的使用
4种类型的自动装配:
byName:把与Bean的属性具有相同名字(或者ID)的其他bean自动装配到Bean的对应属性中。如果没有跟属性的名字相匹配的Bean,则该属性不进行装配。
byType:把与Bean的属性具有相同类型的其他bean自动装配到Bean的对应属性中。如果没有跟属性的类型相匹配的Bean,则该属性不被装配。
constructor:把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中
autodetect:首先尝试使用constructor进行自动装配。如果失败,再尝试使用byType进行自动装配。
byName限制:需要架设Bean的名字与其他Bean的属性的名字一样
byType限制:如果spring徐找到多个Bean,他们的类型与需要自动装配的属性的类型都相匹配,在这种场景下,spring不会猜测哪一个Bean更适合自动装配,而是选择抛出异常。
设置首选Bean,默认bean的primary属性都为true,相当于都是首选Bean,所以需要将不需要的都设置为false。
排除某些Bean,设置bean的autowire-candidate为false
constructor:局限性和byType相同,不会选择。
-->
<bean id="keyny" class="com.bean.Instrumentalist" autowire="byName">
<property name="song" value="Jingle Bells"/>
</bean>
<bean id="keyny2" class="com.bean.Instrumentalist" autowire="byType" primary="false" autowire-candidate="false">
<property name="song" value="Jingle Bells"/>
</bean>
<bean id="keyny3" class="com.bean.Instrumentalist" autowire="constructor">
<property name="song" value="Jingle Bells"/>
</bean>
<bean id="keyny4" class="com.bean.Instrumentalist" autowire="autodetect"/>
</beans>
- spring di2
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Spring
- Spring
- spring
- spring
- spring
- Hadoop学习笔记_伪分布式安装
- mysql简单命令
- 微信公众平台和开放平台的区别
- spring di
- 日经春秋 20150525
- spring di2
- spring aop
- spring datasource jdbctemplate
- 手机浏览器判断是android或者iPhone跳转
- spring hibernate
- spring tx
- 如何判断一棵二叉树是完全二叉树
- 搜索整个首页标题也不见网站的影子,这怎么处理?
- 日经社説 20150525 高齢者の消費者被害を防げ