- 有4种自动装配类型
- byname:在容器(说的应该是spring容器,即是在spring的配置文件中找,如果不是注解方式的话)中寻找和需要自动装配属性名(name)相同的bean或id如果没有找到相符的bean,该属性就没被装配
- 个人理解:默认的好像就是按属性名装配,举例说明
- <?xml version="1.0" encoding="UTF-8"?>
- <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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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-autowire="byType"
- >
- <bean id="kk" class="test.ZTAction" autowire="byName">
- </bean>
- <bean id="p" class="Person">
- </bean>
- </beans>
- 上面这个xml例子里有一个bean kk,他的自动装配属性是按名称装配,也就是说如果类test.ZTAction中含有属性Person p,并且在spring容器中有一个bean的id或name为p,而且它的类型为Person
- 那么类test.ZTAction初始化时,它的属性Person也会初始化
- byType:在容器中寻找一个与需要自动装配的属性类型相同的bean;如果没找到相符的bena,该属性就没有被装配上,如果找到超过一个相符的bean抛出异常
- <?xml version="1.0" encoding="UTF-8"?>
- <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"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-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-autowire="byType"
- >
- <bean class="test.TestAction" >
- <property name="st" >
- <value>ok</value>
- </property>
- </bean>
- <bean id="kk" class="test.ZTAction" autowire="byType">
- </bean>
- </beans>
- 注意:要在被装配的对象上加autowire
- 上面这个xml例子里有一个bean kk 他的自动装配属性是按类型装配,也就是说类test.ZTAction中含有属性test.TestAction,并且在spring容器中有一个bean的class为test.TestAction
- 那么类test.ZTAction初始化时,他的属性test.TestAction也会初始化