spring--注入方式

1.正常方式:

在一个“value”标签注入值,并附有“property”标签结束。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="FileNameGenerator" class="com.yiibai.common.FileNameGenerator">
        <property name="name">
            <value>yiibai</value>
        </property>
        <property name="type">
            <value>txt</value>
        </property>
    </bean>
</beans>

2.快捷方式

注入值“value”属性。
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="FileNameGenerator" class="com.yiibai.common.FileNameGenerator">
        <property name="name" value="yiibai" />
        <property name="type" value="txt" />
    </bean>
    
</beans>

3.p模式

通过使用“p”模式作为注入值到一个属性。

 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <bean id="FileNameGenerator" class="com.yiibai.common.FileNameGenerator" 
             p:name="yiibai" p:type="txt" />
    
</beans>

4.import多个配置文件方式:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <import resource="Spring-Datasource.xml" />
    <import resource="Spring-Customer.xml" />
    <import resource="jdbcTemp.xml" />

</beans>

5.内部bean注入方式

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="CustomerBean" class="com.yiibai.common.Customer">
        <property name="person">
            <bean class="com.yiibai.common.Person">
                <property name="name" value="yiibai" />
                <property name="address" value="address1" />
                <property name="age" value="28" />
            </bean>
        </property>
    </bean>
</beans>

6.内部bean构造器注入:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="CustomerBean" class="com.yiibai.common.Customer">
        <constructor-arg>
            <bean class="com.yiibai.common.Person">
                <property name="name" value="yiibai" />
                <property name="address" value="address1" />
                <property name="age" value="28" />
            </bean>
        </constructor-arg>
    </bean>
</beans>

 

posted on 2017-08-10 17:03  猫儿爹  阅读(161)  评论(0编辑  收藏  举报

导航