autowire属性值 | 具体含义 |
no | 不使用自动装配。必须通过ref元素指定依赖,这是默认设置。显式指定协作者可以使配置更灵活、更清晰,但是ref 方式是的XML体积臃肿维护强度高。 |
byName | 根据属性名自动装配。此选项将检查容器并根据名字(id)查找与属性完全一致的bean,并将其与属性自动装配。 若找不到则不装配。 |
byType | 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。 如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。primary="true" 可以设置优先权 避免异常抛出 |
constructor | 与byType的方式类似,不同之处在于它应用于构造器参数。 如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。 |
autodetect | 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。 如果发现默认的构造器,那么将使用byType方式。 |
default | 同<beans/>级别设定的autowiring策略 |
- package cn.beanfactory.bean;
- public class AutowireBean2 {
- public String toString(){
- return "AutowireBean2";
- }
- }
- package cn.beanfactory.bean;
- public class AutowireBean1 {
- public String toString(){
- return "AutowireBean1";
- }
- }
- package cn.beanfactory.bean;
- public class AutowireBean {
- private AutowireBean1 autowireBean1;
- private AutowireBean2 autowireBean2;
- public AutowireBean(){
- System.out.println("AutowireBean()");
- }
- public AutowireBean(AutowireBean1 autowireBean1, AutowireBean2 autowireBean2) {
- System.out.println("AutowireBean(AutowireBean1 autowireBean1, AutowireBean2 autowireBean2)");
- this.setAutowireBean1(autowireBean1);
- this.setAutowireBean2(autowireBean2);
- }
- public AutowireBean2 getAutowireBean2() {
- return autowireBean2;
- }
- public void setAutowireBean2(AutowireBean2 autowireBean2) {
- this.autowireBean2 = autowireBean2;
- }
- public AutowireBean1 getAutowireBean1() {
- return autowireBean1;
- }
- public void setAutowireBean1(AutowireBean1 autowireBean1) {
- this.autowireBean1 = autowireBean1;
- }
- public String toString(){
- return "autowireBean1:"+autowireBean1+";autowireBean2:"+autowireBean2;
- }
- }
- log.info("============autowire=====================");
- AutowireBean byName = gbfa.getBean("byName");
- AutowireBean byType = gbfa.getBean("byType");
- AutowireBean byConstructor = gbfa.getBean("byConstructor");
- AutowireBean byAutodetect = gbfa.getBean("byAutodetect");
- log.info("byName:"+byName+",\nbyType:"+byType+",\nbyConstructor:"+byConstructor+",\nbyAutodetect:"+byAutodetect);
- <!-- autowire -->
- <bean id="byName" class="cn.beanfactory.bean.AutowireBean" autowire="byName" />
- <bean id="byType" class="cn.beanfactory.bean.AutowireBean" autowire="byType"/>
- <bean id="byConstructor" class="cn.beanfactory.bean.AutowireBean" autowire="constructor"/>
- <bean id="byAutodetect" class="cn.beanfactory.bean.AutowireBean" autowire="autodetect"/>
- <bean id="autowireBean1" class="cn.beanfactory.bean.AutowireBean1" autowire-candidate="true"/>
- <bean id="autowireBean2" class="cn.beanfactory.bean.AutowireBean2"/>
- <bean id="autowireBean2primary" class="cn.beanfactory.bean.AutowireBean2" primary="true"/>
dependency-check 属性
dependency-check属性 | 具体含义 |
none | 不去检查受管Bean的依赖性,这是受管Bean的默认行为 |
simple | 仅仅对JAVA原型(primitive)和集合类型进行检查 |
objects | 仅仅对协作者(对象类型)进行检查 |
all | 同时对Java原型(primitice)、集合类型、协作者(对象类型)进行检查 |
default | 同<beans/>级别指定的一致 |
- AutowireBean autowireBean = gbfa.getBean("autowireBean");
- log.info("byName:"+byName+",\nbyType:"+byType+",\nbyConstructor:"+byConstructor+",\nbyAutodetect:"+byAutodetect+";\nautowireBean:"+autowireBean);
- <!-- dependency-check -->
- <bean id="autowireBean" class="cn.beanfactory.bean.AutowireBean" dependency-check="objects">
- <property name="autowireBean1" ref="autowireBean1"/>
- <property name="autowireBean2">
- <null/><!-- <value/> 赋值 null -->
- </property>
- <property name="str">
- <value/><!-- <value/> 赋值“” -->
- </property>
- </bean>
二、@required 注解
- package cn.beanfactory.bean;
- import javax.xml.ws.FaultAction;
- import org.springframework.beans.factory.annotation.Required;
- public class RequiredBean {
- private HelloWorld hw1;
- private HelloWorld hw2;
- private String str;
- public HelloWorld getHw1() {
- return hw1;
- }
- @Required
- @ForYou
- public void setHw1(HelloWorld hw1) {
- this.hw1 = hw1;
- }
- public HelloWorld getHw2() {
- return hw2;
- }
- @Required
- public void setHw2(HelloWorld hw2) {
- this.hw2 = hw2;
- }
- public String getStr() {
- return str;
- }
- @Required
- public void setStr(String str) {
- this.str = str;
- }
- }
- <!-- @required 注解 提供细粒度的依赖检查-->
- <bean id="requiredAnnotationBeanPostProcessor" class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
- <property name="requiredAnnotationType" value="cn.beanfactory.bean.ForYou"/> <!-- 指定标识 -->
- </bean>
- <bean id="requriedBean" class="cn.beanfactory.bean.RequiredBean">
- <property name="hw1" ref="helloWorld"/>
- <property name="hw2">
- <null/>
- </property>
- <property name="str">
- <value/>
- </property>
- </bean>
- <bean name="helloWorld" class="cn.beanfactory.bean.HelloWorld">
- <property name="helloStr" ref="fileHello"></property>
- </bean>
- <bean name="fileHello" class="cn.beanfactory.bean.FileHelloStrImpl">
- <constructor-arg value="helloWorld.properties" />
- </bean>