6.依赖注入

构造器注入已经讲过

set注入

新建实体类

package com.dream.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
package com.dream.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

常量注入

<bean id="address" class="com.dream.pojo.Address">
        <property name="address" value="四川"/>
    </bean>
<bean id="student" class="com.dream.pojo.Student">
        <property name="name" value="张三"/>
    </bean>

bean注入

<property name="address" ref="address"/>

数组注入

<property name="books">
    <array>
        <value>西游记</value>
        <value>红楼梦</value>
        <value>三国演义</value>
        <value>水浒传</value>
    </array>
</property>

List注入

<property name="hobbys">
            <list>
                <value>听歌</value>
                <value>跑步</value>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>

Map注入

<property name="card">
            <map>
                <entry key="身份证" value="132237785447229"/>
                <entry key="银行卡" value="48823152323230023"/>
            </map>
        </property>

Set注入

<property name="games">
            <set>
                <value>英雄联盟</value>
                <value>星际争霸</value>
                <value>魔兽世界</value>
            </set>
        </property>

Null注入

<property name="wife">
            <null/>
        </property>

Properties注入

<property name="info">
            <props>
                <prop key="driver">2043784</prop>
                <prop key="url"></prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

测试

import com.dream.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("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

拓展注入实现

新建User类

package com.dream.pojo;

public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

P命名空间注入和c命名空间注入都需要在头文件加入约束条件

<?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"
       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">
    <bean id="user" class="com.dream.pojo.User" p:age="20" p:name="张三"/>
    <bean id="user2" class="com.dream.pojo.User" c:age="21" c:name="张三"/>
</beans>

测试

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

Bean的作用域

Singleton:在Spring Ioc容器中仅存在一个Bean实例,Bean以单例方式存在,默认值

Prototype:每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行new xxxBean()

Request:每次HTTP请求都会创建一个新的Bean,该作用域仅适用WebApplicationContext环境

Session:同一个HTTP Session共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationContext

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值