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 >
        <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 >
        <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 > 
             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 >
        <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 >
        <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>

 

相关文章:

  • 2022-12-23
  • 2021-05-27
  • 2022-01-14
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-07
  • 2021-08-18
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
相关资源
相似解决方案