ssm 创建bean的三种方式和spring依赖注入的三种方式

 <!--创建bean的第一种方式:使用默认无参构造函数 在默认情况下:
    它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败-->
    <bean id="service" class="service.Impl.ServiceImpl" scope="prototype"></bean>
    <!--第二种方式,使用普通工厂中的方法创建对象(使用某个类中的方法创建对象并且使用容器)-->
    <bean id="factory" class="factory.InstanceFactory"></bean>
    <bean id="factoryService" factory-bean="factory" factory-method="getService"></bean>
    <!--第三种方法,使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入Spring容器)-->
    <bean id="staticFactory" class="factory.StaticInstance" factory-method="getService"></bean>

spring依赖注入的三种方式

  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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注入的方式:
    1.使用构造函数提供
    2.使用set方法提供(使用较多)
    3.使用注解提供-->
<!--========================   1.使用构造函数提供(使用较少)=============================-->
    <bean id="service" class="service.Impl.ServiceImpl">
        <!--constructor-arg 里面的属性
            type:用于要注入的数据的类型,该类型也是构造函数中某个或者某个的参数类型
            index:用于要注入的数据给构造函数中指定索引位置色参数赋值,索引的位置是从0开始
            name: 用于指定给构造函数中指定名称的参数复制(建议使用)
            =============以上三个用于指定给哪个参数复制=================
            value:用于提供基本数据类型和string的数据
            ref:用于指定其他bean类型数据.它指的是在spring的ioc核心容器中出现过的bean对象

            注意!因为是构造提供所以必须要给value赋值,即使不用到也得提供参数
        -->
        <constructor-arg name="name" value="------测试"></constructor-arg>
        <constructor-arg name="age" value="19"></constructor-arg>
        <constructor-arg name="birthday" ref="now"> </constructor-arg><!--constructor标签不可以编译日期-->
        <!--ref-->
    </bean>
<!--配置日期-->
    <bean id="now" class="java.util.Date"></bean>

</beans>
 2.使用set方法提供(使用较多)
<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注入的方式:
    1.使用构造函数提供(使用较少)
    2.使用set方法提供(使用较多)
    3.使用注解提供-->
<!--========================使用set方法提供(使用较多)=============================-->
    <bean id="service" class="service.Impl.ServiceImpl">
        <!--property标签 bean的内部
            1.name:用于指定注入的所调用的set方法名称
            -->
        <property name="name" value="test"/>
        <property name="age" value="24"/>
        <property name="birthday" ref="now"/>
    </bean>
    <bean id="now" class="java.util.Date"/>
</beans>

  2.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--注入的方式:
    1.使用构造函数提供(使用较少)
    2.使用set方法提供(使用较多)
    3.复杂注入
    //3.使用注解提供-->
<!--======================复杂注入===============================-->
    <bean id="ser" class="service.Impl.ServiceImpl">
        <property name="myStrs">
            <array>
                <value>aaa</value>
                <value>bbb</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>list1</value>
                <value>2</value>
            </list>
        </property>
        <property name="myMap">
            <map>
                <entry key="1" value="啊啊啊"></entry>
                <entry key="2"><value>啊啊啊</value></entry>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key="测试标签体">测试标签体</prop>
            </props>
        </property>
    </bean>
    <!--用于给Map结构注入的标签
            map标签和props结构相同可以互换
        用于给list结构集合注入的标签
            list    array     set 都可以使用
        -->

</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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--基于注解-->
    <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器中 -->
        <context:component-scan base-package="com"/>
</beans>    

 

原文地址:https://www.cnblogs.com/july7/p/12678265.html