3月28
Spring中autowire属性
default-autowire="x"
x有5个选择:byName,byType,constructor和autodetect,no
一、spring 自动装配 default-autowire="byName"
Service.java
public class Service
{
Source source;
public void setSource(Source source)
{
this.source = source;
}
}
applicationContext.xml:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<bean id="source" class="tk.hecks.spring.DBCPSource" scope="prototype"/>
<bean id="service" class="tk.hecks.spring.Service" scope="prototype">
</bean>
</beans>
tk.hecks.spring.DBCPSource实现了Source接口
xml中并没有给 bean service配Source属性,但在beans中设置了default-autowire="byName",这样配置文件会自动根据 tk.hecks.spring.Service 中的setSource找bean id="Source"的bean ,然后自动配上去,如果没找到就不装配。
注意:byName的name是java中setXxxx 的Xxxx, 和上面设置的Source source中source拼写毫无关系,完全可以是
public class Service
{
Source source1;
public void setSource(Source source1)
{
this.source1 = source1;
}
}
结果相同。
byName的方式要保证在Service类中的set方法为setSource,很显然不是很灵活,也就是说要和上面声明的id的值要相同.
二、spring 自动装配 default-autowire="byType"
Service.java同上
applicationContext.xml
<beans
...
default-autowire="byType">
<bean id="dbcpSource" class="tk.hecks.spring.DBCPSource" scope="prototype"/>
<bean id="service" class="tk.hecks.spring.Service" scope="prototype">
</bean>
</beans>
同样没有配置setSource,autowire改成 "byType",配置文件会找实现了Source接口的bean,这里 tk.hecks.spring.DBCPSource 实现了Source接口,所以自动装配,如果没找到则不装配。
如果同个配制文件中两个bean实现了Source接口,则报错。
这里的 Type是指setSource(Source source)中参数的类型。
具体spring bean的自动装配模式详解:
1、byName 是指通过bean的属性名字进行装配,在spring的xml文档中查找与要装配属性相同名称的bean进行装配。
2、byType 是指在spring 的xml中正好有一个与属性类型相同的bean进行自动装配。如果有多于一个这样的bean就抛出一个异常之处不能对那个bean使用自动装配功能。
3、constructor(构造方法):是指根据构造方法中的参数在spring的xml文件中查找相匹配的bean,如果没找到则抛出异常。
4、autodect:通过对bean检查类内部来选择constructor或bytype方式进行注入,先找到constructor就用constructor ,如果没有constructor就用bytype。
5、no就是不使用自动装配。
bean的引用必须通过ref元素定义。很多大型应用不允许使用自动装配。因为它对于bean之间的依赖参考关系不清晰。
三、默认配置是no,推荐用这种,因以上自动装配,对维护不是太好。
来源:Heck's Blog
地址:https://www.heckjj.com/post/421/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
default-autowire="x"
x有5个选择:byName,byType,constructor和autodetect,no
一、spring 自动装配 default-autowire="byName"
Service.java
public class Service
{
Source source;
public void setSource(Source source)
{
this.source = source;
}
}
applicationContext.xml:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<bean id="source" class="tk.hecks.spring.DBCPSource" scope="prototype"/>
<bean id="service" class="tk.hecks.spring.Service" scope="prototype">
</bean>
</beans>
tk.hecks.spring.DBCPSource实现了Source接口
xml中并没有给 bean service配Source属性,但在beans中设置了default-autowire="byName",这样配置文件会自动根据 tk.hecks.spring.Service 中的setSource找bean id="Source"的bean ,然后自动配上去,如果没找到就不装配。
注意:byName的name是java中setXxxx 的Xxxx, 和上面设置的Source source中source拼写毫无关系,完全可以是
public class Service
{
Source source1;
public void setSource(Source source1)
{
this.source1 = source1;
}
}
结果相同。
byName的方式要保证在Service类中的set方法为setSource,很显然不是很灵活,也就是说要和上面声明的id的值要相同.
二、spring 自动装配 default-autowire="byType"
Service.java同上
applicationContext.xml
<beans
...
default-autowire="byType">
<bean id="dbcpSource" class="tk.hecks.spring.DBCPSource" scope="prototype"/>
<bean id="service" class="tk.hecks.spring.Service" scope="prototype">
</bean>
</beans>
同样没有配置setSource,autowire改成 "byType",配置文件会找实现了Source接口的bean,这里 tk.hecks.spring.DBCPSource 实现了Source接口,所以自动装配,如果没找到则不装配。
如果同个配制文件中两个bean实现了Source接口,则报错。
这里的 Type是指setSource(Source source)中参数的类型。
具体spring bean的自动装配模式详解:
1、byName 是指通过bean的属性名字进行装配,在spring的xml文档中查找与要装配属性相同名称的bean进行装配。
2、byType 是指在spring 的xml中正好有一个与属性类型相同的bean进行自动装配。如果有多于一个这样的bean就抛出一个异常之处不能对那个bean使用自动装配功能。
3、constructor(构造方法):是指根据构造方法中的参数在spring的xml文件中查找相匹配的bean,如果没找到则抛出异常。
4、autodect:通过对bean检查类内部来选择constructor或bytype方式进行注入,先找到constructor就用constructor ,如果没有constructor就用bytype。
5、no就是不使用自动装配。
bean的引用必须通过ref元素定义。很多大型应用不允许使用自动装配。因为它对于bean之间的依赖参考关系不清晰。
三、默认配置是no,推荐用这种,因以上自动装配,对维护不是太好。
来源:Heck's Blog
地址:https://www.heckjj.com/post/421/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!
如何在spring中自定
JAVA的动态代理机制和



