SpringのIOCでオブジェクトを作成する3つの方法

1.下付き文字の割り当て

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.tt.pojo.user">
    	 <!--第一,下标赋值-->
        <constructor-arg index="0" value=""/>
    </bean>

</beans>

2.タイプの割り当て

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.tt.pojo.user">
    	 <!--第二种,通过类型创建,不建议使用,重复类型难以分辨-->
        <constructor-arg type="java.lang.String" value=""/>
    </bean>

</beans>

3.パラメータ名の割り当て(推奨)

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.tt.pojo.user">
    	<!--第三种,直接通过参数名来设置-->
        <constructor-arg name="str" value=""/>
    </bean>

</beans>
元の記事を51件公開 ・いい ね73 訪問数3700

おすすめ

転載: blog.csdn.net/qq_41256881/article/details/105423552