依 赖 注 入

依赖注入
1、setter注入

    <bean id="studentService" class="com.tq.service.impl.StudentServiceImpl">
        <!--引用类型注入-->
        <property name="studentDao" ref="studentDao"/>
        <!--基础类型注入-->
        <property name="name" value="studentDao"/>
        <property name="age" value="10"/>
    </bean>
    <bean id="studentDao" class="com.tq.dao.impl.StudentImpl" init-method="init" destroy-method="destory"/>

1.1、类

StudentDao studentDao;
public String name;
public int age;

public void setAge(int age) {
this.age = age;
}

public void setName(String name) {
this.name = name;
}

public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}

2、构造器注入

<!--构造器注入-->
<bean id="studentService" class="com.tq.service.impl.StudentServiceImpl">
    <!--引用类型注入-->
    <constructor-arg name="studentDao" ref="studentDao"/>
    <!--基础类型注入-->
    <constructor-arg name="name" value="张山"/>
    <constructor-arg name="age" value="58"/>
</bean>
<bean id="studentDao" class="com.tq.dao.impl.StudentImpl" init-method="init" destroy-method="destory"/>

2.1、类
StudentDao studentDao;
public String name;
public int age;

public StudentServiceImpl(StudentDao studentDao, String name, int age) {
this.studentDao = studentDao;
this.name = name;
this.age = age;
}

其他方式
type

<!--构造器注入-->
<bean id="studentService" class="com.tq.service.impl.StudentServiceImpl">
    <!--基础类型注入-->
    <constructor-arg type="java.lang.String" value="张SAN"/>
    <constructor-arg type="int"  value="59"/>
</bean>

index

<bean id="studentService" class="com.tq.service.impl.StudentServiceImpl">
    <!--基础类型注入-->
    <constructor-arg index="0" value="张SAN张SAN张SAN张SAN张SAN"/>
    <constructor-arg index="1"  value="5959"/>
</bean>

<bean id="studentDao" class="com.tq.dao.impl.StudentImpl" init-method="init" destroy-method="destory"/>

3、自动装配----只针对引用类型进行自动装配(setter注入)

<!--自动装配-->
<bean id="studentDao" class="com.tq.dao.impl.StudentImpl"/>
<bean id="studentService" class="com.tq.service.impl.StudentServiceImpl" autowire="byType"/>

4、集合注入----(setter注入)

    <property name="map">
        <map>
            <entry key="name" value="张三"/>
            <entry key="age" value="42"/>
            <entry key="sex" value="fal"/>
        </map>
    </property>

    <property name="properties">
        <props>
            <prop key="popName">Tom</prop>
            <prop key="popName">Tom</prop>
            <prop key="popName">Tom</prop>
            <prop key="popAge">12</prop>
            <prop key="popSex">男</prop>
        </props>
    </property>

    <property name="set">
        <set>
            <value>z</value>
            <value>z</value>
            <value>z</value>
            <value>4</value>
            <value>唐</value>
        </set>
    </property>

加载properties文件

properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.1:3306/mybatis
jdbc.name=root
jdbc.password=1234
applicationcontext.xml配置
classpath ---->–当前路径
classpath
---->所有路径
.properties ---->所有properties文
systrm-properties-mode=“NEVER” ---->覆盖系统值*

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

<!--1.开启context命名空间-->

<!--2.使用context空间加载properties文件-->
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

<!--3.使用属性占位符${}读取properties文件中的属性-->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值