Spring的依赖注入方式

1.构造器注入

前面文章已经记录了

    <bean id="getUser" class="com.cbbpp.pojo.User">
        <constructor-arg index="0" value="土拨鼠"/>
    </bean>

    <bean id="getUser" class="com.cbbpp.pojo.User">
    <constructor-arg type="java.lang.String" value="周一"/>
    </bean>

    <bean id="getUser" class="com.cbbpp.pojo.User">
    <constructor-arg name="name" value="yiyi"/>
    </bean>

2.Set方式注入

第一步:创建pojo--->Student.java,Teacher.java

package com.cbbpp.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] book;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wifi;
//getter and setter and toString
}
package com.cbbpp.pojo;

public class Address {
    private String address;
//getter and setter and toString
}

 第二步:applicationContext.xml

<?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="address" class="com.cbbpp.pojo.Address">
        <property name="address" value="长沙"/>
    </bean>
    <bean id="student" class="com.cbbpp.pojo.Student">
        <!--第一种:普通注入-->
        <property name="name" value="土拨鼠"/>
        <!--第二种:Bean注入使用ref-->
        <property name="address" ref="address"/>
        <!--第三种:数组注入-->
        <property name="books">
            <array>
                <value>数组</value>
                <value>数组</value>
                <value>数组</value>
            </array>
        </property>
        <!--第四种:list注入-->
        <property name="hobby">
            <list>
                <value>list</value>
                <value>list</value>
                <value>list</value>
            </list>
        </property>
        <!--第五种:map注入-->
        <property name="card">
            <map>
                <entry key="性别" value="男"/>
                <entry key="身高" value="177"/>
                <entry key="年龄" value="22"/>
            </map>
        </property>
        <!--第六种:null注入-->
        <property name="wifi">
            <null/>
        </property>
        <!--第七种:property注入-->
        <property name="info">
            <props>
                <prop key="driver">org.mysql.jdbc.driver</prop>
                <prop key="url">sadasdasdas</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
        <!--第八种:set注入-->
        <property name="games">
            <set>
                <value>11111</value>
                <value>11111</value>
                <value>11111</value>
            </set>
        </property>
    </bean>
</beans>

测试和结果:

import com.cbbpp.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
/*
        Student{
            name='土拨鼠', 
            address=Address{address='长沙'}, 
            book=[数组, 数组, 数组], 
            hobby=[list, list, list], 
            card={性别=男, 身高=177, 年龄=22}, 
            games=[11111], 
            info={password=root, 
            url=sadasdasdas, driver=org.mysql.jdbc.driver, username=root}, 
            wifi='null'}

*/
    }
}

3.拓展方式注入

p-namespace(property属性注入)

默认调用pojo里的无参构造器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

//加入p命名空间约束
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
//p标签使用
    <bean id="user" class="com.cbbpp.pojo.User" p:name="土拨鼠" p:age="22"/>

</beans>

 

c-namespace(constracter构造器注入)

要在pojo里添加有参构造器

<?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:p="http://www.springframework.org/schema/p"
//加入c命名空间约束
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
//c标签的使用
    <bean id="user2" class="com.cbbpp.pojo.User" c:age="18" c:name="aaa1"/>
    
</beans>

 测试:

    @Test
    public void user(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
        User user = (User) context.getBean("user2");
        System.out.println(user);
    }

注意:p命名和c命名空间不能直接使用,需要导入xml约束

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值