Spring中IOC创建对象的三种方式

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
今日推荐