spring <bean>的自动装配

来源:互联网 发布:seo工程师考证 编辑:程序博客网 时间:2024/04/29 01:20

1.不做任何操作

<bean id="serrvice" class="com.serviceImpl">

<propery  name="dao" ref="Adao"/>

</bean>

<bean id="Adao" class="com.dao"/>

2.byName 根据属性名自动装配(default-autowire="byName"),此选项将检查容器并根据名字查找与属性完全一致的bean,并将与其属性自动装配;

<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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
        default-autowire="byName">

<bean id="serrvice" class="com.serviceImpl"/>

<bean id="Adao" class="com.dao"/>

</beans>

3

<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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
        default-autowire="byType">

<bean id="serrvice" class="com.serviceImpl"/>

<bean " class="com.dao"/>

</beans>

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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
        default-autowire="Constructor">

<bean id="serrvice" class="com.serviceImpl"/>

<bean " class="com.dao"/>

</beans>