初始Spring(贰)

目录

3,HelloSpring

4,IOC创建对象的方式 

5,Spring配置 

5.1,别名

5.2,Bean的配置

5.3,import

6,依赖注入 

6.1,构造器注入

6.2,set方法注入[重点]


3,HelloSpring

创建Hello类:

package com.xiao.domain;

public class Hello {
    private String str;

    public String getStr() {
        return str;
    }

    public Hello() {
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Hello(String str) {
        this.str = str;
    }

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

 配置bean:

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

    <!-- 使用spring来创建对象,在Spring中这都称为Bean
        类型 变量名=new 类型();
        Hello hello=new Hello();
        bean=对象   new Hello();

        id=变量名
        class=new的对象
        property=给对象中的属性设置一个值
     -->
    <bean id="hello" class="com.xiao.domain.Hello">
        <property name="str" value="Spring"/>
    </bean>

</beans>

测试:

import com.xiao.domain.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //获取Spring的上下文对象!
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象都在Spring中管理了我们要使用对象之间取出来就可以了
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

结果:

Hello{str='Spring'}

思考问题?

  • Hello对象是谁创建的?

        Hello对象是由Spring创建的

  • Hello对象的属性是怎么设置的? 

        Hello对象的属性是由Spring设置的

这个过程就叫做控制反转;

控制:谁来控制对象的创建,传统应用程序是由程序本身来控制创建的!使用Spring后对象是由Spring创建的.

反转:程序本身不创建对象,而变成被动的接收对象 

依赖注入:就是利用set方法进行依赖注入的

IOC是一种编程思想:由主动编程变成被动接收

可以通过new ClassPathXmlApplicationContext去浏览一下Spring源码

OK,到了现在我们彻底不用在程序中去修改了!要实现不同的操作,只需要在xml配置文件中进行修改,所谓的ioc一句话搞定:对象由Spring来,创建,管理,装配!

4,IOC创建对象的方式 

  1. 使用无参构造创建对象,默认!
  2. 假设我们需要使用有参构造创建对象:
    1. 下标赋值:                                                                                                                          
          <bean id="user" class="com.xiao.domain.User">
              <!-- index:传入构造器参数的下标 value:传入的值 -->
              <constructor-arg index="0" value="小步"/>
          </bean>
    2. 通过类型匹配                                                                                                                    
          <!-- 类型匹配(不建议使用) -->
          <bean id="user" class="com.xiao.domain.User">
              <!-- type:构造器传入值的类型 value:传入的值 -->
              <constructor-arg type="java.lang.String" value="小步"/>
          </bean>
    3. 通过参数名匹配                                                                                                                 
          <!-- 参数名匹配 -->
          <bean id="user" class="com.xiao.domain.User">
              <!-- type:构造器传入值的类型 value:传入的值 -->
              <constructor-arg name="name" value="小步"/>
          </bean>

总结:当ClassPathXmlApplicationContext();加载xml文件时会创建其中全部配置的对象!

扩展:Spring底层实现是采用了一个Map进行键值对的存储的!

5,Spring配置 

5.1,别名

    <!-- 别名:如果添加了别名我们可以使用别名去获取对象 -->
    <alias name="user" alias="newUser"/>

5.2,Bean的配置

    <!--
     id:唯一的标识符,代表我们的变量名
     class: bean对于的类必须包名+类名
     name: 起的别名并且可以起多个可以用空格逗号等分割
     -->
    <bean id="testUser" class="com.xiao.domain.TestUser" name="test test1">
        <property name="name" value="小步"/>
    </bean>

5.3,import

 这个import一般用于团队开发使用,他可以将多个配置文件合并为一个。

假设现在有多人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个,使用的时候之间使用总配置就可以了!

  • applicationContext.xml
    <import resource="beans1.xml"/>
    <import resource="beans2.xml"/>
    <import resource="beans3.xml"/>

6,依赖注入 

6.1,构造器注入

前面已经说过了于4.ioc创建方式!

6.2,set方法注入[重点]

[环境搭建]

复杂类型:

package com.xiao.domain;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

测试真实对象

package com.xiao.domain;

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;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + 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;
    }
}

beans.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="student" class="com.xiao.domain.Student">
        <!-- 第一种普通值注入 -->
        <property name="name" value="小步"/>
    </bean>

</beans>

测试类:

import com.xiao.domain.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student.getName());
    }
}

完善注入信息:

<?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.xiao.domain.Address">

    </bean>

    <bean id="student" class="com.xiao.domain.Student">
        <!-- 第一种普通值注入 -->
        <property name="name" value="小步"/>
        <!-- 第二种注入bean注入:ref -->
        <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>
            </list>
        </property>
        <!-- 第五种Map注入 -->
        <property name="card">
            <map>
                <entry key="身份证" value="15826714480"/>
                <entry key="银行卡" value="13597826637"/>
            </map>
        </property>
        <!-- 第六种Set注入 -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>MC</value>
                <value>CF</value>
            </set>
        </property>
        <!-- 第七种:null(空)值注入 -->
        <property name="wife">
            <null/>
        </property>

        <!-- 第八种:Properties值注入 -->
        <property name="info">
            <props>
                <prop key="学号">2106</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>


</beans>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

123小步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值